如何从ios中的Facebook SDK 4.0获取用户名

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

您无法再获取用户名:

/ me / username不再可用。

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

如果要检测返回的用户,请改用(App Scoped)ID。

-(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,你可以从这里轻松搞定。 注册您的应用创建密钥并在您的应用程序中导入此密钥。 快乐的编码

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

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

*使用我的代码,它的工作非常好。

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