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

jay*_*via 3 iphone facebook ios

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

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

}
Run Code Online (Sandbox Code Playgroud)

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


lus*_*chn 6

你不能再得到用户名了:

/ me/username不再可用.

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

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