如何从 iOS 中的 Facebook SDK 获取用户信息?

Posted

技术标签:

【中文标题】如何从 iOS 中的 Facebook SDK 获取用户信息?【英文标题】:How to get user info from Facebook SDK in iOS? 【发布时间】:2014-03-01 08:22:28 【问题描述】:

如何在用户会话打开时获取用户名。我尝试了 Facebook sdk 示例中的会话登录示例。如果有人知道解决方案,请帮助我。 提前致谢。

 - (IBAction)buttonClickHandler:(id)sender 
        // get the app delegate so that we can access the session property
        SLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

        // this button's job is to flip-flop the session from open to closed
        if (appDelegate.session.isOpen) 
            // if a user logs out explicitly, we delete any cached token information, and next
            // time they run the applicaiton they will be presented with log in UX again; most
            // users will simply close the app or switch away, without logging out; this will
            // cause the implicit cached-token login to occur on next launch of the application
   //hear i need to fetch user name ?


            [appDelegate.session closeAndClearTokenInformation];

         else 
            if (appDelegate.session.state != FBSessionStateCreated) 
                // Create a new, logged out session.
                appDelegate.session = [[FBSession alloc] init];
            

            // if the session isn't open, let's open it now and present the login UX to the user
            [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                             FBSessionState status,
                                                             NSError *error) 
                // and here we make sure to update our UX according to the new session state
                [self updateView];
            ];
        
    

【问题讨论】:

developers.facebook.com/docs/ios/graph 【参考方案1】:

登录验证后,您可以从活动的 FBSession 中获取详细信息,如下所示

if (FBSession.activeSession.isOpen) 

    [[FBRequest requestForMe] startWithCompletionHandler:
     ^(FBRequestConnection *connection,
       NSDictionary<FBGraphUser> *user,
       NSError *error) 
         if (!error) 
             NSString *firstName = user.first_name;
             NSString *lastName = user.last_name;
             NSString *facebookId = user.id;
             NSString *email = [user objectForKey:@"email"];
             NSString *imageUrl = [[NSString alloc] initWithFormat: @"http://graph.facebook.com/%@/picture?type=large", facebookId];
         
     ];

更新:在 v3.14.1(2014 年 5 月 12 日)发布后,id 属性已被弃用,而不是 id 使用 objectID 属性

NSString *facebookId = user.objectID;

【讨论】:

@PeerMohamedThabib 你得到了什么错误?你有回电吗? 你说的 fbfetchRequest 是什么意思? 工作得很好,谢谢 最新的 Facebook SDK 建议使用 user.objectID 而不是 user.id('id' 已被弃用,我想是为了避免与 Objective-c id 对象冲突) @Pravin 我没有检查最新的 sdk。如果您知道 sdk 的确切版本,请随时更新我的​​答案。【参考方案2】:

您好,您可以获取 Facebook 用户的详细信息,例如:

- (void)sessionStateChanged:(NSNotification*)notification 
    if ([[FBSession activeSession] isOpen]) 
        [FBRequestConnection
         startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                           id<FBGraphUser> user,
                                           NSError *error) 
             if (! error) 

                //details of user
                NSLog(@"User details =%@",user);
             
         ];
    else 
        [[FBSession activeSession] closeAndClearTokenInformation];
    

谢谢

【讨论】:

【参考方案3】:
if (!error && state == FBSessionStateOpen)
    NSLog(@"Session opened");
    // Show the user the logged-in UI
     FBRequest* userupdate = [FBRequest requestWithGraphpath:@"me" parameters: [NSMutableDictionary dictionaryWithObject:@"picture.type(large),id,birthday,email,gender,username,name,first_name" forKey:@"fields"] HTTPMethod:@"GET"];
    [drk showWithMessage:nil];
    [userupdate startWithCompletionHandler: ^(FBRequestConnection *connection,
                                          NSDictionary* result,NSError *error)
   
    NSLog(@"dict = %@",result);
   ];
    return;

【讨论】:

【参考方案4】:
        FBSDKGraphRequest(graphPath: "me", parameters: nil).startWithCompletionHandler( (connection, object, error) in
            guard let user = object as? Dictionary<String, NSObject> else 
                alert("facebook sdk gave me trash object \(object)")
                return
            
            if let error = error 
                alert(error.localizedDescription)
            
            else 
                if let userID = user["id"] 
                   // gr8t success

                

            
        )

【讨论】:

【参考方案5】:
-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user 
  
    
    NSLog(@"user facebook details:%@",user.objectID);
    
   

你可以实现这个委托方法。

【讨论】:

以上是关于如何从 iOS 中的 Facebook SDK 获取用户信息?的主要内容,如果未能解决你的问题,请参考以下文章

如何从 facebook ios sdk 3.1 中提取 facebook 电子邮件?

我如何从 facebook 公开资料 Facebook IOS Swift SDK 中获取性别

如何从 Facebook iOS sdk 获取好友列表

如何从 iOS 上的 Facebook SDK 获取用户数据

使用 facebook SDK for ios 6 实现 Facebook 注销

使用 Facebook SDK Login (iOs/Android) 登录 Facebook @ Web?