【问题标题】:How to fetch Facebook user information in ios如何在 ios 中获取 Facebook 用户信息
【发布时间】:2014-08-12 05:40:18
【问题描述】:

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

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。”

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

【问题讨论】:

    标签: ios facebook fetch


    【解决方案1】:

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

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

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

     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 也将获得调用,那时你会得到结果,因为它是一个异步调用。

    如果还是不行,试试这个

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

    【讨论】:

    • @Pavimaa 至少你能做的就是投票给我的答案并标记它是正确的。
    • 有没有什么简单的方法可以把这个字典传给另一个类??我试过了。但它总是会显示 null ..
    • @pavimaa 创建一个属性字典,在这里设置它并在任何其他类中获取它。
    【解决方案2】:

    `(void)fbAccountConfigureWithBlock:(void (^)(id, NSString *))block { _block_data=block;

    if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self showAlertMessage:@"" message:@"Please go to settings and add at least one facebook account."];
            _block_data(nil,nil);
        });
        return;
    }
    
    ACAccountStore *store = [[ACAccountStore alloc]init];
    ACAccountType *accountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    
    [store requestAccessToAccountsWithType:accountType
                                   options:@{ACFacebookAppIdKey         : FacebookAppId,
                                             ACFacebookAudienceKey      : ACFacebookAudienceFriends,
                                             ACFacebookPermissionsKey   : @[@"email"]}
                                completion:^(BOOL granted, NSError *error)
     {
         if(granted){
             NSArray *array = [store accountsWithAccountType:accountType];
             if(!array.count){
                 dispatch_sync(dispatch_get_main_queue(), ^{
                     [self showAlertMessage:@"" message:@"Please go to settings and add at least one facebook account."];
                     _block_data(nil,nil);
                 });
             }
             else{
                 ACAccount *account = array[0];
                 SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                         requestMethod:SLRequestMethodGET
                                                                   URL:[NSURL URLWithString:@"https://graph.facebook.com/me"]
                                                            parameters: @{@"fields":@"id,first_name,last_name,name,email,picture.height(180).width(180)"}];
                 [request setAccount:account];
    
                 [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
                  {
                      if(!error){
                          NSDictionary *userData = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
                          NSLog(@"Facebook user data ----> %@",userData);
                          dispatch_async(dispatch_get_main_queue(), ^{
                              if(userData[@"error"] != nil)
                                  [self attemptRenewCredentials:store account:account];
                              else
                                  _block_data(userData,nil);
                          });
                      }
                      else{
                          dispatch_async(dispatch_get_main_queue(), ^{
                              [self showAlertMessage:@"" message:error.localizedDescription];
                              _block_data(nil,nil);
                          });
                      }
                  }];
             }
         }
         else
         {
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self showAlertMessage:@"" message:@"We need permission to access your facebook account in order make registration."];
                 _block_data(nil,nil);
             });
         }
     }];
    

    }`

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多