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

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

问题描述

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

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

[FBSession openActiveSessionWithReadPermissions:permission
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:请求到端点的错误我':一个开放的FBSession必须被指定给这个端点的调用。



提前感谢,非常感谢你的帮助。

解决方案

错误是非常合适的,该尝试说的是在会话打开后应该调用请求连接方法。
现在您的

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

方法返回BOOL值true或false以指定您的会话是否打开(尝试打开同步)。所以首先检查这个调用的结果,并将其放在代码中以获取信息。例如,如果(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也将被调用,当时您将获得结果,因为它是异步调用。



如果仍然无效,请尝试此

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

}
else
{
//在这里检索用户的详细信息,如下所示
NSLog(@FB用户名:%@, user.first_name);
NSLog(@FB用户名:%@,user.last_name);
NSLog(@FB用户生日:%@,user.birthday);
}
}];


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"]);
    }];

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) {
                              }];

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"]);
}];

}

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天全站免登陆