You have developed an application in iOS and now you want to make it “Social”. Facebook and Twitter are the two most popular social networking sites. In this tutorial, we will learn how to integrate Facebook in your application.
1. Register an Application With Facebook
To register an iOS application with Facebook, go to http://developer.facebook.com and follow steps given below.
- Tap on Apps then tap Create New App button.
- A popup window will appear where you will enter Application Name and tap on continue.
- A Screen will come with your Application detail which you have given now in this page you have to fill some more information. Tap the section Native iOS App here fill the Bundle identifier of application so that Facebook server will communicate with your application with the help of this identifier.
- Make sure Sandbox mode is disabled so that your application will be live and visible to all users.
- Copy the App Id generated by Facebook for further use.
2. Now open your iOS project which you have created and do some changes
- Open the plist file and Add one row with key FacebookAppId and give the value which you have copied from Facebook (See Step #1 above)
- Add one more row with key URL Types which type will be Array and it contains one item of types Dictionary. This Dictionary will contain one item with Key URL Scheme of type Array. This array will contain one string with key item 0 and its value will be your appId which you have copied with prefix name fb.
3. Required Frameworks
- Facebook sdk Framework
- Social Framework
- Accounts Framework
- AdSupport Framework
4. Go to Project Build Setting and add Two linker flag.
5. Write the Facebook Login code (Say On Button Click event handler)
NSArray *permissions = [NSArray arrayWithObjects:@"email",nil];
[FBSession setActiveSession: [[FBSession alloc] initWithPermissions:permissions ] ];
[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error)
NSString *token = session.accessTokenData.accessToken;
NSLog(@"Error is %@",[error description]);
In this code snippet, first I have declared an Array which contains the permissions and then set the permission to FBSession object. Now open the Session by calling the method name openWithBehavior:completionHandler. When this method will execute it will open Facebook Login UI Page where user will enter their credential for login.
Understanding Facebook Session State Engine
When we create FBSession state object it migrate one state to another. So it is very important to know how it switches one state to another state. See the diagram below:
1. Created (FBSessionStateCreated) Initial states indicating that no valid cached token was found. 2. Created Token Loaded (FBSessionStateCreatedTokenLoaded):
Initial session states indicating that a cached token was loaded. When a session is in this state, a call to open will result in an open session without UX.
3. Opening (FBSessionStateCreatedOpening): Pre-open session states indicating that an attempt to open the session is under-way. 4. Opened (FBSessionStateOpen): Open session state indicating user has logged in or a cached token is available. 5. Open Token Expanded (FBSessionStateOpenTokenExtended): Open session state indicating token has been extended. 6. Closed Login Failed (FBSessionStateClosedLoginFailed): Closed session state indicating that a login attempt failed. 7. Closed (FBSessionStateClosed):
Closed session state indicating that the session was closed, but the users token remains cached on the device for later use.
Looking for a mobile solution with social media integration? Contact firstname.lastname@example.org