Getting user's Personal info from Facebook in iOS

前端 未结 4 1038
悲哀的现实
悲哀的现实 2020-12-08 00:50

I am quite new to objective-C and iPhone Development environment.

I am implementing Facebook login in my app to get User\'s name, Email and profile Picture. I have s

4条回答
  •  借酒劲吻你
    2020-12-08 01:45

    Sorry for this messy answer, this is my first answer ever. You can use FBSDK Graph request to fetch user's all profile infos and FBSDKProfilePictureView class to fetch user's Profile Picture easily.This code is for manually Facebook login UI.

    Firstly, you must put this code where login process start:

     FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    
    [login logInWithReadPermissions:@[@"public_profile", @"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
    
        if (error)
        {
    
         // There is an error here.
    
        }
        else
        {
            if(result.token)   // This means if There is current access token.
            {    
                // Token created successfully and you are ready to get profile info
                [self getFacebookProfileInfo];
            }        
        }
    }]; 
    

    And If login is successfull, implement this method to get user's public profile;

    -(void)getFacebookProfileInfos { 
    
    FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:nil];
    
    FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
    
    [connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
    
          if(result)
          {            
            if ([result objectForKey:@"email"]) {
    
              NSLog(@"Email: %@",[result objectForKey:@"email"]);
    
            }
            if ([result objectForKey:@"first_name"]) {
    
              NSLog(@"First Name : %@",[result objectForKey:@"first_name"]);
    
            }
            if ([result objectForKey:@"id"]) {
    
              NSLog(@"User id : %@",[result objectForKey:@"id"]);
    
            }
    
          }
    
     }];
    
    [connection start];
    

    Get current logged in user's profile picture:

    FBSDKProfilePictureView *pictureView=[[FBSDKProfilePictureView alloc]init];
    
    [pictureView setProfileID:@"user_id"];
    
    [pictureView setPictureMode:FBSDKProfilePictureModeSquare];
    
    [self.view addSubview:pictureView];
    

    You must add refreshing code to your viewDidLoad method:

       [FBSDKProfile enableUpdatesOnAccessTokenChange:YES];
    

提交回复
热议问题