Facebook Integration in iOS_InnovationM

692 views

Published on

Learn how to integrate Facebook with iOS applications.

http://blogs.innovationm.com/facebook-integration-in-ios/

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
692
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Facebook Integration in iOS_InnovationM

  1. 1. | Build a Mobile Experience www.innovationm.com Facebook Integration in iOS Corporate Office: InnovationM Mobile Technologies E-3 (Ground Floor), Sector-3, Noida 201301 (India) t: +91 8447 227337 | e: info@innovationm.com
  2. 2. | Build a Mobile Experience www.innovationm.com Facebook Integration in iOS 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. Corporate Office: InnovationM Mobile Technologies E-3 (Ground Floor), Sector-3, Noida 201301 (India) t: +91 8447 227337 | e: info@innovationm.com
  3. 3. | Build a Mobile Experience www.innovationm.com 3. Required Frameworks  Facebook sdk Framework  Social Framework  Accounts Framework  AdSupport Framework 4. Go to Project Build Setting and add Two linker flag.  Sqlite3.0  ObjC 5. Write the Facebook Login code (Say On Button Click event handler) - (IBAction)handler:(id)sender { NSArray *permissions = [NSArray arrayWithObjects:@"email",nil]; [FBSession setActiveSession: [[FBSession alloc] initWithPermissions:permissions ] ]; [[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSessio n *session, FBSessionState status, NSError *error) { if(!error) { NSString *token = session.accessTokenData.accessToken; } else { 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. Corporate Office: InnovationM Mobile Technologies E-3 (Ground Floor), Sector-3, Noida 201301 (India) t: +91 8447 227337 | e: info@innovationm.com
  4. 4. | Build a Mobile Experience www.innovationm.com 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. Corporate Office: InnovationM Mobile Technologies E-3 (Ground Floor), Sector-3, Noida 201301 (India) t: +91 8447 227337 | e: info@innovationm.com
  5. 5. | Build a Mobile Experience www.innovationm.com 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. Corporate Office: InnovationM Mobile Technologies E-3 (Ground Floor), Sector-3, Noida 201301 (India) t: +91 8447 227337 | e: info@innovationm.com

×