【问题标题】:How to get username from Facebook SDK 4.0 in ios如何在 iOS 中从 Facebook SDK 4.0 获取用户名
【发布时间】:2015-06-09 23:13:19
【问题描述】:

如何在 iOS 中从 facebook sdk 4.0 获取 用户名

【问题讨论】:

    标签: ios iphone facebook


    【解决方案1】:
    -(IBAction)LoginWithFacebook:(id)sender {
    
        if ([FBSDKAccessToken currentAccessToken]) {
    
            [self getDetailsAndLogin];
    
        }
        else{
            FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
            [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
                if (error) {
                    // Process error
                    NSLog(@"%@",error.description);
                } else if (result.isCancelled) {
                    // Handle cancellations
                    NSLog(@"Result Cancelled!");
                } else {
                    // If you ask for multiple permissions at once, you
                    // should check if specific permissions missing
    
                    if ([result.grantedPermissions containsObject:@"email"]) {
                        // Do work
                        [self getDetailsAndLogin];
    
                    }
                }
            }];
    
        }
    
    }
    
    -(void)getDetailsAndLogin{
        if (LOGGING) {
            return;
        }
        LOGGING = YES;
        [super startLoader];
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error) {
                 NSString *userID = [[FBSDKAccessToken currentAccessToken] userID];
                 NSString *userName = [result valueForKey:@"name"];
                 NSString *email =  [result valueForKey:@"email"];
                 NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", [[FBSDKAccessToken currentAccessToken] userID]];
    
                 [User LoginWithFbId:userID Username:userName Email:email ImageUrl:userImageURL success:^(User *response) {
                     [super stopLoader];
                     UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                     TabViewController *TabVC = [sb instantiateViewControllerWithIdentifier:@"TabViewController"];
                     [self.navigationController pushViewController:TabVC animated:YES];
    
                 } failure:^(NSString *error) {
                     LOGGING = NO;
                     [super stopLoader];
                     [super showAlertWithTitle:@"Cannot Login" Message:error];
                 }];
             }
             else{
                 LOGGING = NO;
                 [super stopLoader];
                 NSLog(@"%@",error.localizedDescription);
             }
         }];
    
    }
    

    这里的LoginWithFacebook 是一个获取数据的按钮动作。不要忘记导入 FBSession 的 SDK,您可以从 here 轻松获得。注册您的应用程序创建一个密钥并将此密钥导入您的应用程序。 快乐编码

    【讨论】:

      【解决方案2】:

      您无法再获取用户名:

      /me/username 不再可用。

      来源:https://developers.facebook.com/docs/apps/changelog#v2_0_graph_api

      如果您想检测回访用户,请改用(App Scoped)ID。

      【讨论】:

        【解决方案3】:

        最简单的答案是在用户登录后检查以下内容:

        if ([FBSDKProfile currentProfile]) 
        { 
            NSLog(@"User name: %@",[FBSDKProfile currentProfile].name);
            NSLog(@"User ID: %@",[FBSDKProfile currentProfile].userID);
        }
        

        【讨论】:

        • 这可能不一定是安全的(因为我已经学会了艰难的方式)。即使 currentAccessToken 不为 null,currentProfile 有时也没有完成获取。在这种情况下,您可能会得到一个空配置文件。
        • if ([FBSDKProfile currentProfile]) { NSLog(@"用户名:%@",[FBSDKProfile currentProfile].name); NSLog(@"用户ID:%@",[FBSDKProfile currentProfile].userID); } 添加此检查以避免 null
        【解决方案4】:

        *使用我的代码,效果很好。

        - (IBAction)tapon_facebookLogin:(id)sender {
            if ([FBSDKAccessToken currentAccessToken]) {
            // TODO:Token is already available.
               NSLog(@"FBSDKAccessToken alreay exist");
               [self fetchFbUserInfo];
        
        }else{
            NSLog(@"FBSDKAccessToken not exist");
            FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
            [loginManager logInWithReadPermissions:@[@"email",@"public_profile"]
                                fromViewController:self
                                           handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
                                               //TODO: process error or result
                                               if (!error) {
                                                   NSLog(@"result %@",result.debugDescription);
                                                   [self fetchFbUserInfo];
                                               }else{
                                                   NSLog(@"errorfacebook %@",error.description);
                                               }
                                           }];
        }}
        
        
        
        
        -(void)fetchFbUserInfo{
        
        if ([FBSDKAccessToken currentAccessToken])
        {
            NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
            [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday ,location ,friends ,hometown , friendlists"}]
             startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                 if (!error)
                 {
                     NSLog(@"resultisfetchFbUserInfo:%@",result);
                 }
                 else
                 {
                     NSLog(@"ErrorfetchFbUserInfo %@",error);
                 }
             }];}}
        

        【讨论】:

        • 确实很棒!
        猜你喜欢
        • 1970-01-01
        • 2013-09-21
        • 2011-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多