Avatar

Not applicable

1. General iOS Setup 

Before you add Facebook Login you need to complete the "Getting Started" guide for the Facebook SDK for iOS. Once completed, come back to this doc.

Facebook SDK for iOS - Getting Started

2. Add Login Kit to your Xcode Project

  1. Open ~/Documents/FacebookSDK
  2. Drag the FBSDKLoginKit to Frameworks in Project Navigator.

3. Add Facebook Login Button Code

To add a Facebook-branded login button to your app add the following code snippet to a view controller.

// Add this to the header of your file, e.g. in ViewController.m // after #import "ViewController.h"#import <FBSDKCoreKit/FBSDKCoreKit.h>#import <FBSDKLoginKit/FBSDKLoginKit.h>// Add this to the body@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];// Optional: Place the button in the center of your view.loginButton.center = self.view.center;[self.view addSubview:loginButton];}@end

Screenshots: Login & Logout Flow

Compile and run your app. If your implementation was completed correctly you should be able to replay the following login and logout flow in your app:

4. Custom Login Button Code

Instead of using the Facebook-branded login button you may want to design a custom layout and behavior. In the following code example we invoke the login dialog using the FBSDKLoginManager and a custom UIButton. You can use any other custom user interface or event handling to invoke the login dialog.

// Add this to the header of your file#import "ViewController.h"#import <FBSDKCoreKit/FBSDKCoreKit.h>#import <FBSDKLoginKit/FBSDKLoginKit.h>// Add this to the body@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Add a custom login button to your appUIButton *myLoginButton=[UIButton buttonWithType:UIButtonTypeCustom];myLoginButton.backgroundColor=[UIColor darkGrayColor];myLoginButton.frame=CGRectMake(0,0,180,40);myLoginButton.center = self.view.center;[myLoginButton setTitle: @"My Login Button" forState: UIControlStateNormal];// Handle clicks on the button[myLoginButton addTarget:selfaction:@selector(loginButtonClicked) forControlEvents:UIControlEventTouchUpInside];// Add the button to the view[self.view addSubview:myLoginButton];}// Once the button is clicked, show the login dialog-(void)loginButtonClicked{FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];[login logInWithReadPermissions: @[@"public_profile"]handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {if (error) {NSLog(@"Process error");} else if (result.isCancelled) {NSLog(@"Cancelled");} else {NSLog(@"Logged in");}}];}@end

5. Asking for Permissions

When you add Facebook Login, your app can ask someone for permissions on a subset of that person's data.

Read Permissions for Facebook Login Button

For the Facebook-branded login button (FBSDKLoginButton) use the readPermissions property to request additional read permissions.

In your view header file add:

//  ViewController.h#import <FBSDKLoginKit/FBSDKLoginKit.h>@interface ViewController : UIViewController@property (weak, nonatomic) IBOutlet FBSDKLoginButton *loginButton;@end

In your view file add:

// Extend the code sample "1. Add Facebook Login Button Code"// In your viewDidLoad method:loginButton.readPermissions =@[@"public_profile", @"email", @"user_friends"];

Your app will now ask for the person's email address and friend list.

http://www.besanttechnologies.com/training-courses/java-training | http://www.besanttechnologies.com/company/corporate-training#