如何在iOS中使用Facebook SDK 4.0获取用户名

3
如何在iOS中使用Facebook SDK 4.0获取用户名
4个回答

9
-(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,你可以从这里轻松获取。注册你的应用程序,创建一个密钥并在你的应用程序中导入此密钥。 祝编码愉快!


6

4

用户登录后,最简单的解决方法是检查以下内容:

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

2
这可能并不安全(我已经吃过亏了)。即使 currentAccessToken 不为 null,currentProfile 有时仍未完成获取。在这种情况下,您可能会得到一个空的 profile。 - JCutting8
如果 ([FBSDKProfile currentProfile]) { NSLog(@"用户名: %@",[FBSDKProfile currentProfile].name); NSLog(@"用户ID: %@",[FBSDKProfile currentProfile].userID);}添加此检查以避免空值。 - user1969245

2

*使用我的代码,它可以完美运行。

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

真是太棒了! - manuelBetancurt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接