如何在iOS中使用Facebook SDK 4.1获取用户信息? [英] how can i get user information using facebook sdk 4.1 in ios?

查看:93
本文介绍了如何在iOS中使用Facebook SDK 4.1获取用户信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Facebook集成版本2.3的预构建登录UIButton.

I'm using a pre-built login UIButton of Version 2.3 of facebook integration.

问题:在获取用户详细信息时获取空结果.

Problem: Getting null result, when fetching user details.

-(void) loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error
{
    if ([FBSDKAccessToken currentAccessToken]) {

            FBSDKGraphRequest *request =[[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:nil];
            [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                          id result,
                                          NSError *error)
    {
        // Handle the result
        NSLog(@"%@",result);
    }];

    FBSDKProfile *profile = [[FBSDKProfile alloc]init];

    }
}

推荐答案

尝试以下代码:

AppDelegate.m

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [FBSDKAppEvents activateApp];


     //Default FB Button
    [[NSNotificationCenter defaultCenter]postNotificationName:@"getFacebookData" object:nil];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation];
}

viewController的ViewDidLoad

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getFacebookData) name:@"getFacebookData" object:nil];

 FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
 loginButton.center = self.view.center;
 [self.view addSubview:loginButton];

在视图控制器中添加此方法

- (void)getFacebookData{
    if ([FBSDKAccessToken currentAccessToken]) {
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"first_name, last_name, picture.type(large), email, name, id, gender"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error) {
                 NSLog(@"fetched user:%@", result);
             }
         }];
    }
}

这篇关于如何在iOS中使用Facebook SDK 4.1获取用户信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆