Wednesday, 19 March 2014

iPhone Devlopment: Get user profile information from Facebook in IOS






Facebook is a common social platform that connects million of user with each other. So it's a best way to get your app user basic info from Facebook rather then letting him/her to fill out a signup form. In IOS we can fetch user profile information using the native SDK provided by Facebook.
First download the SDK form Facebook developer website.
https://developers.facebook.com/docs/ios/

Now install this SDK on your machine, it will get installed under Document folder, which is located inside your mac user folder.
Add FacebookSDK.framework to your project, you can do this by following steps
Select your project, chose target, and select  general tab.
Scroll down until you reached Linked Frameworks and Libraries section.(Shown in below figure)

Click on + sign then a drop down will appear, select Add other.(Shown in below figure)
Go to document folder, select FacebookSDK.framework.
Now add additional frameworks that are required by FacebookSDk.framework.These are
  • Accounts.framework
  • Social.framework
  • AdSupport.framework
  • SystemConfiguration.framework
  • libsqlite3.0.dylib



As you complete all the steps to integrate FacebookSDK.framework, import FacebookSDk in to your viewcontroller
#import
Now compile and run your project, if it compiles successfully the you are on right direction  otherwise you miss a  step and re-follow the steps listed above.
Create an app on developer.facebook.com and get its app id. Open your .plist and add your Facebook App id there with a key FacebookAppID. You can add a new key by clicking + sign.
Now cretae a URL scheme for your app so that when user goes for facebook authnetication on safari, control can came back to your app after user selection for the options listed by Facebook.
Both of these things are shown in below pictures


Add your facebook Id in front of FacebookAppID key and in url scheme add your Facebook key with fb is appended as prefix.
Suppose your app id is 76472487237462, then your FacebookAppID = 76472487237462.
And under URL types, in front of item , you have to enter fb76472487237462.

Now open up your AppDelegate.m file and in your applicationDidBecomeActive method add below written line
 [FBSession.activeSession handleDidBecomeActive];

In your applicationWillTerminate method write below line , it will close your active session for Facebook
[FBSession.activeSession close];

Now it's time to implement Facebook, that will update our Facebook Session if user logging successfully



- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    return [FBSession.activeSession handleOpenURL:url];
}

Now its time to get logged-in user profile info
Below is the code that do same

 if ([[FBSession activeSession]isOpen])
        {
            [self showUserInfo];
 }
        else
        {
            [FBSession openActiveSessionWithReadPermissions:@[@"public_profile",@"email",@"user_location",
                                                              @"user_birthday",
                                                              @"user_likes",@"read_friendlists"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error)
            {
                if (!error)
                {
                   
                    [FBSession setActiveSession:session];
                    [self showUserInfo];
                }
                else
                {
    }
            }];

        }
-(void)showUserInfo
{
    FBRequest* friendsRequest = [FBRequest requestForMe];
    [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                                  NSDictionary* result,
                                                  NSError *error)
     {
          if (!error)
          {
               NSLog(@"reulst == %@",result);
          }
      }];
}

In the above code, first we checked if our session is open or closed.If its open then we will fetch user info otherwise we will open a session for Facebook with read permissions.If there is no error then we will set our active session to current session and then call our method that will get user info for us. For getting user info we will use requestForMe method provided in FBRequest class of Facebook SDK.


No comments:
Write comments