Newest Post

Showing posts with label facebook. Show all posts
Showing posts with label facebook. Show all posts

Facebook SDK 3.1 integration with iOS 6.0

| Friday, December 28, 2012
Read more »

The Facebook SDK for iOS is designed to be easy to install and use. Following are the steps which can be followed to integrate Facebook SDK in iOS 6.0

  1. Prerequisites
    • Install Latest version of Xcode or at least Xcode 4.5
    • Install Facebook SDK for iOS 6. The default install location is "Documents/FacebookSDK"
  2. SDK includes an Xcode project which contains sample code for separate modules linked with Facebook SDK.
  3. To run an app with Facebook, We need to mention Facebook App Id, for that a new Facebook App needs to be created using Facebook personal/developer account.
  4. Create an Xcode project to implement Facebook SDK.
  5. Add frameworks "FacebookSDK","Accounts","AdSupport" and "Social" into the frameworks section (using build phases -> link libraries) of Test App Xcode project which we created in Step 4.
  6. Add "libsqlite3.dylib" library also in the frameworks section from the build phases -> link libraries.
  7. Add the FacebookAppId, which we created on Facebook website, in the Xcode App plist file as follows

  • Start Coding!!

  • Facebook Login Setup

    The Facebook SDK for iOS will automatically choose the best option when you attempt to use Facebook login, i.e either through the Facebook App on iPhone or opening the mobile Facebook in the Safari browser.

    1. Set up an Xcode project, here we created a single view application project named "FacebookTest".

    2. In AppDelegate.h (whatever name in your project), Add code to include Facebook SDK

    3. Add a button in the View Controller to initiate the login flow. Wire this button to an action named authButtonAction in header file of View Controller.

        - (IBAction)authButtonAction:(id)sender;

    4. In AppDelegate.h define a global variable for Facebook notification and action definition to initiate a session.

    extern NSString *const FBSessionStateChangedNotification;

    - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI; 

    5. In AppDelegate.m, define the notification string

    NSString *const FBSessionStateChangedNotification = @"com.example.Login:FBSessionStateChangedNotification";

    6. Also add the 3 methods in AppDelegate.m file

     * Callback for session changes.
    - (void)sessionStateChanged:(FBSession *)session
                          state:(FBSessionState) state
                          error:(NSError *)error
        switch (state) {
            case FBSessionStateOpen:
                if (!error) {
                    // We have a valid session
                    NSLog(@"User session found");
            case FBSessionStateClosed:
            case FBSessionStateClosedLoginFailed:
                [FBSession.activeSession closeAndClearTokenInformation];
        [[NSNotificationCenter defaultCenter]
        if (error) {
            UIAlertView *alertView = [[UIAlertView alloc]
            [alertView show];

     * Opens a Facebook session and optionally shows the login UX.
    - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
        NSArray *permissions = [[NSArray alloc] initWithObjects:
        return [FBSession openActiveSessionWithReadPermissions:permissions
                                             completionHandler:^(FBSession *session,
                                                                 FBSessionState state,
                                                                 NSError *error) {
                                                 [self sessionStateChanged:session

     * If we have a valid session at the time of openURL call, we handle
     * Facebook transitions by passing the url argument to handleOpenURL
    - (BOOL)application:(UIApplication *)application
                openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication
             annotation:(id)annotation {
        // attempt to extract a token from the url
        return [FBSession.activeSession handleOpenURL:url];

    The first method called openSessionWithAllowLoginUI: is invoked when the login button is clicked or when a check is made for an active session when the main view is initially loaded. This method calls the openActiveSessionWithReadPermissions:allowLoginUI:completionHandler: method of FBSession class to open a Facebook session. 

    The second method called sessionStateChanged: defines the completion handler. 


    gives additional permissions for email and user likes.

    application:openURL:sourceApplication:annotation: delegate method calls the Facebook session object:

    7. handleDidBecomeActive: handles the call to the active session of Facebook. Call this method in AppDelegate.m file's  applicationDidBecomeActive method. 

    - (void)applicationDidBecomeActive:(UIApplication *)application
        // We need to properly handle activation of the application with regards to Facebook Login
        // (e.g., returning from iOS 6.0 Login Dialog or from fast app switching).
        [FBSession.activeSession handleDidBecomeActive];
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    8. Call close method of FBSession class to close the current active Facebook session when App (FacebookTest) is about to terminate. 

    - (void)applicationWillTerminate:(UIApplication *)application
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        [FBSession.activeSession close];


    9. Include AppDelegate.h in the ViewController implementation file.

    10. Call the openSessionWithAllowLoginUI in the authButtonAction action

    - (IBAction)authButtonAction:(id)sender {
        AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        // The user has initiated a login, so call the openSession method
        // and show the login UX if necessary.
        [appDelegate openSessionWithAllowLoginUI:YES];

    Build and run the project, If everything is added correctly the App should run in the simulator. If not please 
    1. check the required frameworks have been added properly or not
    2. check FacebookAppId is added in plist and Id does exist.

    Facebook SDK 3.1 integration with iOS 6.0

    Posted by : Pooja
    Date :Friday, December 28, 2012
    With 2comments
    Tag : ,