In-App Facebook Authentication on iOS

by

The official Facebook iOS SDK provides an easy way for any iOS applications to authenticate against a Facebook account. If you follow this tutorial, you’ll be able to add facebook authentication in your app in less than 15 minutes. It’s that simple. Once your app has been authenticated, it will possess both access token and expiration date. These two pieces of data will be used in subsequent communication with Facebook.

From your app’s user’s perspective, this authentication process involves the following steps:

1. Put the app in the background
2a. If Facebook app is not installed on the iOS device, launch Safari and open Facebook login page.
2b. If Facebook app is installed, launch Facebook app.
3. Once user logged in, either Safari or Facebook app will launch your app.

These steps seem to be fairly straight-forward. It’s all done by authorize method:

 if (!) { ; } 

However, some may find it annoying to experience multiple app-switching. And worse, the only place that your code gets a chance to receive access token and expiration date information is at the app delegate:

 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return ; } 

If you have a complex app and the part that needs Facebook authentication is buried deep inside several layers of features, it may be challenging to directly pass this authentication information. These are just a few reasons to consider Facebook authentication that doesn’t involve switching of apps. Such in-app facebook authentication can be found in several popular apps, such as Flipboard and Photogene.

Fortunately, Facebook iOS SDK has already provided this type of in-app authentication. It’s just not being defined publicly. If you look at the source code, in particular Facebook.h, you’ll see the method signature for authorize:

 - (void)authorize:(NSArray *)permissions; 

authorize calls a private method, authorizeWithFBAppAuth:

 - (void)authorize:(NSArray *)permissions { self.permissions = permissions; [self authorizeWithFBAppAuth:YES safariAuth:YES]; } 

To make an in-app authentication, you can simply call authorizeWithFBAppAuth with both parameters set to NO:

 [self authorizeWithFBAppAuth:NO safariAuth:NO]; 

One way to modify this behavior is to subclass Facebook and override the authorize method. Another way is to directly call authorizeWithFBAppAuth. Since this is not a public method, you’ll get a compiler warning. To suppress the warning, you can call it thru performSelector:

 if ([_facebook respondsToSelector:@selector(authorizeWithFBAppAuth:safariAuth:)]) [_facebook performSelector:@selector(authorizeWithFBAppAuth:safariAuth:) withObject:NO withObject:NO]; 

Here’s the screenshot of Facebook authorization dialog within an app:

9az03ztw7kpe

 

4 Comments

  1. Pingback: Facebook login from internal IOS webpage | Yaiva Answers

  2. Sam on said:

    The only problem with this approach is that it requires the user to enter their login information. If the user (like most users) has the Facebook app installed you can skip this process entirely, using the tokens and custom URLs.

  3. Santo on said:

    You pass a delegate (FBSessionDelegate) when creating Facebook object:
    [[Facebook alloc] initWithAppId:@”xxxx” andDelegate:self];
    The delegate can implement fbDidNotLogin and fbDidLogin.

  4. Martin Harkins on said:

    Good article, I didn’t even know we could have the In-App authentication with facebook.
    Can you pass in a delegate selector to return to after the facebook authentication is complete?

Leave a Reply

Your email address will not be published. Required fields are marked