首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从iOS中的Facebook获取用户信息?

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

Stack Overflow用户
提问于 2014-03-01 08:22:28
回答 5查看 23.5K关注 0票数 9

当用户会话打开时,如何获取用户名,我尝试了来自Facebook示例的会话登录示例。如果有人知道解决办法,请帮帮我。提前谢谢。

代码语言:javascript
复制
 - (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];
            }];
        }
    }
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-03-01 09:28:10

登录身份验证之后,您可以从active FBSession获得详细信息,如下所示

代码语言:javascript
复制
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];
         }
     }];
}

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

代码语言:javascript
复制
NSString *facebookId = user.objectID;
票数 32
EN

Stack Overflow用户

发布于 2014-03-01 09:02:09

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

代码语言:javascript
复制
- (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];
    }
}

谢谢

票数 6
EN

Stack Overflow用户

发布于 2014-03-01 09:12:16

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22111993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档