Friday 4 December 2015

Fetch user profile data after login into Facebook

It is easy to integrate Facebook  login in an iOS app . To integrate Facebook login , first of all we have to configure out app at developer site of the Facebook and  get and app ID which will be used in Xcode configuration . 

We can follow these easy steps form developer site of the Facebook. 

Here I am emphasizing how to get the user profile data once user is successfully login in an app from Facebook.

Example:  First create an IBAction  for a button to login Facebook. and define the method to login and fetch data.

  
    if ([FBSDKAccessToken currentAccessToken]) {
     // 1 user is already login, handle it.
    }else{
    
     //2 log in to Facebook first time
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login
     logInWithReadPermissions: @[@"public_profile",@"email",@"user_about_me"]
     fromViewController:self
     handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
         if (error) {
          
         //3 handle error   
             NSLog(@"Process error %@", error.localizedDescription);
             
         } else if (result.isCancelled) {
             
             NSLog(@"Cancelled");
             
         } else {
        //4login success, proceed next steps
             
             if ([FBSDKAccessToken currentAccessToken])
             {
                 
                 [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"picture,name,location, email"}]
                  startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                      
                      if (!error) {
                          NSLog(@"fetched user:%@", result);
                          
                          //fetch email and profile picture
                          NSString *email = [result objectForKey:@"email"];
                          NSString *user_name = [result objectForKey:@"name"];
                         
                          NSString *pictureURL = [[[result objectForKey:@"picture"]objectForKey:@"data"]objectForKey:@"url"];

                          NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:pictureURL]];
                          
                          
                     }else{
                          NSLog(@"error in fetching user data==%@", error.localizedDescription);
                      }
                  }];
             
             }
            
         }
     }];
    
    }

Now lets describe the above code:

1)We are checking, if user is already login , we don't follow login process.
2)If user is not login through Facebook, then first login him.Here we have to assign some permission that we want to access his profile during login .

      [login logInWithReadPermissions:       @[@"public_profile",@"email",@"user_about_me"]

3) In completion handler, we check for error, cancel of the login process and success of the login and handle accordingly.

4) If login is success,  we get an access token from Facebook . Here we check for access token , if it is , then we fetch the  required data.

No comments:

Post a Comment