如何在 ios 中获取 Facebook 用户信息 [英] How to fetch Facebook user information in ios

查看:45
本文介绍了如何在 ios 中获取 Facebook 用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个简单的应用程序,当用户连接到它时,它会从 Facebook 检索数据.我为此尝试了此代码.

I am trying to develop a simple app, which, retrieves data from Facebook, when the user connects to it. I tried this code for it.

NSArray *permissions = [[NSArray alloc] initWithObjects:@"user_birthday",@"user_hometown",@"user_location",@"email",@"basic_info", nil];

    [FBSession openActiveSessionWithReadPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:^(FBSession *session,
                                                      FBSessionState status,
                                                      NSError *error) {
                                  }];

    [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        NSLog(@"%@", [result objectForKey:@"gender"]);
        NSLog(@"%@", [result objectForKey:@"hometown"]);
        NSLog(@"%@", [result objectForKey:@"birthday"]);
        NSLog(@"%@", [result objectForKey:@"email"]);
    }];

但是当我运行这段代码时,它给出了一个错误FBSDKLog:对端点'me'的请求错误:必须为调用这个端点指定一个打开的FBSession."

But when I run this code, it gives an error "FBSDKLog: Error for request to endpoint 'me': An open FBSession must be specified for calls to this endpoint."

提前致谢,非常感谢您的帮助.

Thanks in advance, really appreciate your help.

推荐答案

这个错误非常贴切,它想说的是,一旦会话打开,就应该调用请求连接方法.现在你的

The error is very appropriate, what it is trying to say is that request connection method should be called once the session is open. Now your

[FBSession openActiveSessionWithReadPermissions:permissions
                                   allowLoginUI:YES
                              completionHandler:^(FBSession *session,
                                                  FBSessionState status,
                                                  NSError *error) {
                              }];

方法返回 BOOL 值 true 或 false 来指定会话是否打开(它尝试同步打开).因此,首先检查此调用的结果并将其放入代码中以获取信息.例如.

method returns BOOL value true or false to specify you wether session is open or not(it tries to open synchronously). So first check the result of this call and the put it inside the code for fetching info. For eg.

 if (FBSession.activeSession.isOpen)
{
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    NSLog(@"%@", [result objectForKey:@"gender"]);
    NSLog(@"%@", [result objectForKey:@"hometown"]);
    NSLog(@"%@", [result objectForKey:@"birthday"]);
    NSLog(@"%@", [result objectForKey:@"email"]);
}];

}

这应该会消除您的错误,但您仍然可能无法获得结果.您可能会或可能不会在第一次调用此代码时获得结果,但无论何时调用完成处理程序的代码,此方法 FBRequestConnection 也将获得调用,那时你会得到结果,因为它是一个异步调用.

This should remove your error, but you still may not get the results.You may or may not get result on the very first call to this code but whenever the code for completion handler will be called, this method FBRequestConnection will also get called and at that time you'll get the results as it is an asynchronous call.

如果还是不行,试试这个

If it still doesn't work try this

 if (FBSession.activeSession.isOpen)
    {
        [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
            if (error)
            {
                NSLog(@"error:%@",error);

            }
            else
            {
                // retrive user's details at here as shown below
                NSLog(@"FB user first name:%@",user.first_name);
                NSLog(@"FB user last name:%@",user.last_name);
                NSLog(@"FB user birthday:%@",user.birthday);
            }
}];

这篇关于如何在 ios 中获取 Facebook 用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆