iOS engineer who likes clean, simple designs and sans-serif.

Loading

iOS Facebook Login [Cheatsheet]

Below are some notes for getting Facebook Login to work on iOS 7.

  1. Create Facebook App

    – Log into Facebook Developers at https://developers.facebook.com/apps and click “Create New App”.
    – Type in App Name and continue.
    – Expand Native iOS App and update the bundle identifier (in Xcode workspace, click on Project => General => Identity)
    – Set “Sandbox Mode” to “disabled”.
    – Set “Facebook Login” to “enabled”.
    – This app should be now live.

  2. Integrating Facebook in iOS Application

    – Edit the Podfile with the line:

      pod 'Facebook-iOS-SDK'

    – Run from the command line “pod install”
    – Initialize Facebook in LLAppDelegate.m:

      [PFFacebookUtils initializeFacebook];

    – In *Info.plist, do the following:
    Add new row “FacebookAppID”
    Add new row “FacebookDisplayName”
    Add new row “URL Types”
    Change URL Types => Item 0 => “URL identifier” to “URL Schemes”
    – In Facebook, get the AppID and DisplayName and update the values in *Info.plist. For URL Schemes, use the AppID preceded by “fb”.
    – Add the following method to the LLAppDelegate.m:

      -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
      {
          return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication withSession:[PFFacebookUtils session]];
      }

  3. [Optional] Installing Facebook Manually

    – Download the SDK from https://developers.facebook.com/docs/ios/getting-started
    – Launch the SDK installer to install it to your documents.
    – Copy the Facebook SDK.framework to Frameworks in Xcode.

  4. Implementing the Login Functionality

    – Create a button and link the IBAction with the following:

      NSArray *permissionsArray = @[@"user_about_me", @"user_interests", @"user_relationships", @"user_birthday", @"user_location", @"user_relationship_details"];
     
      [PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
          self.activityIndicator.hidden = YES;
          [self.activityIndicator stopAnimating];
         
          if (!user) {
              if (!error) {
                  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Log In Error" message:@"The Facebook Login Was Cancelled" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
                  [alertView show];
              } else {
                  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Log In Error" message:[error description] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
                  [alertView show];
              }
          } else {
              [self performSegueWithIdentifier:@"loginToTabBarSegue" sender:self];
          }
      }];

Leave a Comment