Facebook SDK for mobile commerce applications

Facebook mobile SDK for iOS applications

Social integration use cases in mobile commerce apps

Extending on the subject of mcommerce and mobile SDK integrations this post will be about the value your mobile commerce application can receive from direct integration with social networks.

We will review integration options with the strongest social network out there – Facebook, who has a native SDK (software development kit) available for both iOS and Android mobile platforms.

First part of this post will examine the potential use cases and user flows in mobile commerce application and the second part will examine actual implementation code needed for integrating current version of Facebook SDK library into iOS mobile app (to be used on iPhone and iPad devices).

Most online commerce buyers are social about the items they buy and like. They are sharing links and information about the purchases, they are recommending items to their friends, building wishlists, using social communities to organize group purchases and so on.

It is important you enable such functionality and user experience in mobile commerce application. It will not only give your potential customers a chance to share, it also will give your mobile application or product increased visibility in social space resulting more downloads and more active users.

API’s and 3rd party application interactions became part of Facebook since 2007 once it has opened selected platform APIs for developers. For social network this was great increase in opportunities to constantly integrate with new devices (enable external developers to perform these integrations).

Times have moved on since 2007 and Facebook is now days very much concentrating on enabling integrations from mobile devices. All new versions of iOS (starting with iOS6) are tightly integrated with Facebook platform and Facebook engineering team has developed native mobile libraries to simplify such integrations. They act like wrappers around existing Facebook Graph API’s. We are going to take a look at iOS version of this library as iPhone and iPad device users currently remain the main spenders once it comes to mobile shopping.

What does Facebook SDK enable for mobile commerce application?

What value would Facebook SDK bring

Once integrated with Facebook your mobile application can post messages to users news feed. News feed is something what each Facebook user sees after loging in to this social network and which has her friends activity in it. Next, your app can share content on users Facebook timeline. Facebook login, enables your mobile commerce application to receive authentication and authorization from the user by introducing simple login with Facebook credentials step. And as you will see in the next part of this post, Facebooks tight integration into iOS (since iOS 6) means that if user is already loged in to Facebook through their native application or web browser he will not have to login twice.
If application user agrees to share the data, your mobile commerce application can also retrieve details of his friends, likes and other social interests.

Facebook SDK has built in methods to access and use Graph API which do simplify and reduce the coding needed to communicate with API endpoints. Mobile app can have Share on Facebook buttons, after the item is purchased, Recommend to a friend buttons if application user found some item he thinks might well suit their friends. List of users Facebook friends can also be obtained through Graph API.

Facebook SDK integration. How To?

Facebook SDK integration. How to

After looking at the potential use cases in previous chapter this part of the post is about actual implementation.

I will outline how to integrate most recent version of Facebook SDK for iOS to your mobile commerce project, by enabling Facebook login, retrieving friend list of logged in user and enabling Recommend to your friend button for Facebook recommendations.

Remaining part of this post will be somewhat technical and will help you to understand the average effort required to integrate Facebook iOS SDK into mobile application project.

Library itself is hosted in GitHub repository. It has several sample iOS applications in it, with integration code already written, and such examples do always serve as good reference. But for this tutorial we will do integration from very start.

To import library in to iOS project you will have to drag FacebookSDK.framework directory from the Facebook SDK bundle you just downloaded into XCode. If you are a tidy person, you will place it into the Framework section of the file structure.

Next you have to add Facebook application id to the main .plist configuration file of your iOS project. In most cases its Application name -Info.plist configuration file. Facebook application id is something you get after creating application on Facebook platform.

In the controller where you are going to use Facebook SDK you have to import SDK header

 

#import <FacebookSDK/FacebookSDK.h>

 

In our apps we manage FBSession object through the main application delegate. This allows for all controllers in the app to access this property, keep it up to date (once user logs in / logs out). For this controller just has to reference to the main app delegate like this:

 

// Getting reference to the app delegate in order
// to access its session property
MainAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

// If active session is open we will retrieve
// user details and move required controller
// on the stack
if (appDelegate.session.isOpen) {
  // FBSession is currently open
}

 

To check if user has already loged in with Facebook (if Facebook SDK session has active token) you can use the following peace of code

 

// See if the app has a valid token for the current state.
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
    // FBSession has valid token,
    // your app can show logged in view
    // and perform activities on behalf of user
} else {
    // No valid token.
    // You have to display the login page
    // and obtain the token.
}

 

Now, once you have checked if FBSession has token (and if it does you are ready to perform Facebook data requests on behalf of application user) the following code lets you present Facebook login dialog UX. Login dialog is displayed and controlled by Facebook SDK but you can set some preferences in the code.

 

[FBSession openActiveSessionWithReadPermissions:nil
                                   allowLoginUI:YES
                              completionHandler:
                                   ^(FBSession *session, 
                                     FBSessionState state, NSError *error) {
              [self sessionStateChanged:session state:state error:error];
}];
Here,

 

FBSession object is passed the message openActiveSessionWithReadPermissions: with settings. The allowLoginUI one decides if we want to show login UX and completionHandler has code block to be executed once user finishes login through Facebook. Note, you can set allowLoginUI to NO and use this method for checking if active session is open for current app user.

Facebook SDK will first try to check if user account is configured on iOS natively (since iOS 6 its an option every user has), if not it will fallback to native Facebook application or web authentication by using Safari mobile browser.

There are many methods to retrieve and upload user data via Facebook SDK and you will find all these on their developer site. For example if your application wants to publish post on the users Facebook feed, the following code will help

 

[FBRequestConnection
     startWithGraphPath:@"me/feed"
     parameters:self.postParams
     HTTPMethod:@"POST"
     completionHandler:^(FBRequestConnection *connection,
                         id result,
                         NSError *error) {

// Perform your actions based on the 
// result of the posting
}

 

Finally, in your application delegate you should add the code to close the FBSession once application is being closed by user

 

- (void)applicationWillTerminate:(UIApplication *)application {
    // Close the session before quitting
    // this is a good idea because things may be hanging off the session, that need
    // releasing (completion block, etc.) and other components in the app may be awaiting
    // close notification in order to do cleanup
    [FBSession.activeSession close];
}

 

This completes my quick summary of development steps and effort required for integrating Facebook SDK to iOS mobile commerce applications. Much more technical information to dig into you can find on Facebook developers portal.

Get in touch if you have any questions about using social network API’s in mobile commerce apps.

Did you like this post?
Subscribe to our API first monthly newsletter!

No Responses

Post Your Comment

Your email address will not be published.