Skip to content
iOSTutorialJunction
iOSTutorialJunction

Hub of iOS app development tutorials

  • Home
  • Contact Us
  • Privacy Policy
  • Portfolio

iPhone Devlopment: Get user profile information from Facebook in IOS

/ Facebook, Facebook login, Facebook SDK, ios, iPad, iPhone, Xcode / By Aman Aggarwal

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.

← Previous Post
Next Post →

Connect on topmate

Search

iOS Developer jobs

Follow on Facebook

Archives

Copyright © 2026 iOSTutorialJunction | Powered by Astra WordPress Theme
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT