how can i get user information using facebook sdk 4.1 in ios?

主宰稳场 提交于 2020-01-15 06:43:27

问题


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

    }
}

回答1:


try this code:

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

ViewDidLoad of viewController

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

Add this method in view controller

- (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:


Please follow this, you will get your answer To get profile picture -

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

To get profile picture and email information

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


来源:https://stackoverflow.com/questions/30256089/how-can-i-get-user-information-using-facebook-sdk-4-1-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!