如何在 ios 中使用 facebook sdk 4.1 获取用户信息?

Posted

技术标签:

【中文标题】如何在 ios 中使用 facebook sdk 4.1 获取用户信息?【英文标题】:how can i get user information using facebook sdk 4.1 in ios? 【发布时间】:2015-05-15 09:33:37 【问题描述】:

我正在使用 facebook 集成 2.3 版的预构建登录 UIButton。

问题:获取用户详细信息时得到空结果。

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

    

【问题讨论】:

如果结果为零,那么肯定有错误,你能记录一下吗? if (!error) NSLog(@”fetched user:%@”, result); else NSLog(@”error=%@",error) 是的,我可以登录和注销,但无法获取用户详细信息...我使用 self.loginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];权限对吗? 权限是对的,但我说你在这里记录错误。只需设置一个断点记录错误 2015-05-15 15:28:05.278 FacebookLoginDemo[27415:190601] 操作无法完成。 (com.facebook.sdk.core 错误 7。) 这是我的视图确实加载... 【参考方案1】:

试试这个代码:

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

【讨论】:

【参考方案2】:

请按此操作,您将得到答案 获取头像 -

FBSDKProfilePictureView *profilePictureview = [[FBSDKProfilePictureView alloc]initWithFrame:_imageView.frame];
[profilePictureview setProfileID:result[@"id"]];
[self.view addSubview:profilePictureview];

获取头像和邮箱信息

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@@"fields": @"picture, email"] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id res, NSError *error)
 
    if (!error) 
        NSString *pictureURL = [NSString stringWithFormat:@"%@",[res objectForKey:@"picture"]];

        NSLog(@"the EMail = %@", [res objectForKey:@"email"]);

        NSData  *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:pictureURL]];
        _imageView.image = [UIImage imageWithData:data];
    
    else
    
         NSLog(@"%@", [error localizedDescription]);
    ];

【讨论】:

以上是关于如何在 ios 中使用 facebook sdk 4.1 获取用户信息?的主要内容,如果未能解决你的问题,请参考以下文章