Go to https://developers.facebook.com
Create new app
Go to Dashboard
Add product
Select Facebook login
Go to Client OAuth Settings
Client OAuth Login = YES
Web OAuth Login = YES
Go to Facebook Login -> Quick Start -> iOS
Download SDK OR Go to below link
https://origincache.facebook.com/developers/resources/?id=facebook-ios-sdk-current.zip
Go to in your Xcode Project add below four framework
- Bolts.framework
- FBSDKLoginKit.framework
- FBSDKCoreKit.framework
- FBSDKShareKit.framework
Add your Bundle Identifier
Enable Single Sign On = YES
Right-click your .plist
file and choose “Open As Source Code”
Copy and paste the following XML snippet into the body of your file ( <dict>...</dict>
)
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb135257030378906</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>135257030378906</string>
<key>FacebookDisplayName</key>
<string>Cherish Gold iOS</string>
If you use any of the Facebook dialogs (e.g., Login, Share, App Invites, etc.) that can perform an app switch to Facebook apps, your application’s .plist
also need to handle this.
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
Go to Xcode
AppDelegate.h
#import <FBSDKCoreKit/FBSDKCoreKit.h>
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
// Override point for customization after application launch.
return YES;
}
– (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation
];
// Add any custom logic here.
return handled;
}
ViewController.h
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>
<FBSDKLoginButtonDelegate>
– (void)viewDidLoad {
[super viewDidLoad];
//Check if User loggedin with FB or not
if ([FBSDKAccessToken currentAccessToken]) {
NSLog(@”Account Logged in with FB”);
}
else{
NSLog(@”Please Login with FB”);
}
FBSDKLoginButton *facebookLoginButton = [[FBSDKLoginButton alloc] init];
facebookLoginButton.frame = CGRectMake(100, 100, 300, 50);
facebookLoginButton.delegate = self;
[self.view addSubview:facebookLoginButton];
// Do any additional setup after loading the view, typically from a nib.
}
– (IBAction)buttonActionFacebookLoginwithSafariorFacebookApp:(id)sender {
FBSDKLoginManager *fBSDKLoginManager = [[FBSDKLoginManager alloc]init];
fBSDKLoginManager.loginBehavior = FBSDKLoginBehaviorBrowser;
[fBSDKLoginManager logInWithReadPermissions:@[@”email”] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(@”%@”,error);
}
else if (result.isCancelled) {
NSLog(@”result isCancelled”);
}
else {
NSString *stringToken = result.token.tokenString;
if ([result.grantedPermissions containsObject:@”email”]) {
[[[FBSDKGraphRequest alloc]initWithGraphPath:@”me” parameters:@{@”fields”:@”id, name, first_name, last_name, gender, picture.type(large), email”}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@”%@”,result);
NSMutableDictionary *dictionaryFbSDKResponce = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
stringToken,@”toekn”,
[result valueForKey:@”id”],@”id”,
[result valueForKey:@”name”],@”name”,
[result valueForKey:@”first_name”],@”first_name”,
[result valueForKey:@”last_name”],@”last_name”,
[result valueForKey:@”gender”],@”gender”,
[result valueForKeyPath:@”picture.data.url”],@”picture”,
[result valueForKey:@”email”],@”email”, nil];
NSLog(@”%@”,dictionaryFbSDKResponce);
//Navigate to HomeScreenViewController
}
else {
NSLog(@”%@”,[error localizedDescription]);
NSLog(@”%@”,error);
}
}];
}
}
}];
}
– (IBAction)buttonActionFacebookLoginwithPopup:(id)sender {
FBSDKLoginManager *fBSDKLoginManager = [[FBSDKLoginManager alloc]init];
fBSDKLoginManager.loginBehavior = FBSDKLoginBehaviorWeb;
[fBSDKLoginManager logInWithReadPermissions:@[@”email”] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(@”%@”,error);
}
else if (result.isCancelled) {
NSLog(@”result isCancelled”);
}
else {
NSString *stringToken = result.token.tokenString;
if ([result.grantedPermissions containsObject:@”email”]) {
[[[FBSDKGraphRequest alloc]initWithGraphPath:@”me” parameters:@{@”fields”:@”id, name, first_name, last_name, gender, picture.type(large), email”}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@”%@”,result);
NSMutableDictionary *dictionaryFbSDKResponce = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
stringToken,@”toekn”,
[result valueForKey:@”id”],@”id”,
[result valueForKey:@”name”],@”name”,
[result valueForKey:@”first_name”],@”first_name”,
[result valueForKey:@”last_name”],@”last_name”,
[result valueForKey:@”gender”],@”gender”,
[result valueForKeyPath:@”picture.data.url”],@”picture”,
[result valueForKey:@”email”],@”email”, nil];
NSLog(@”%@”,dictionaryFbSDKResponce);
//Navigate to HomeScreenViewController
}
else {
NSLog(@”%@”,[error localizedDescription]);
NSLog(@”%@”,error);
}
}];
}
}
}];
}
#pragma mark – FBSDKLoginButtonDelegate
– (void)loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error {
//After Secessful login this method will fire
NSLog(@”%@”,loginButton);
NSLog(@”%@”,result);
NSLog(@”%@”,error);
if (error) {
NSLog(@”%@”,error);
}
else if (result.isCancelled) {
NSLog(@”result isCancelled”);
}
else {
NSString *stringToken = result.token.tokenString;
if ([result.grantedPermissions containsObject:@”email”]) {
[[[FBSDKGraphRequest alloc]initWithGraphPath:@”me” parameters:@{@”fields”:@”id, name, first_name, last_name, gender, picture.type(large), email”}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@”%@”,result);
NSMutableDictionary *dictionaryFbSDKResponce = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
stringToken,@”toekn”,
[result valueForKey:@”id”],@”id”,
[result valueForKey:@”name”],@”name”,
[result valueForKey:@”first_name”],@”first_name”,
[result valueForKey:@”last_name”],@”last_name”,
[result valueForKey:@”gender”],@”gender”,
[result valueForKeyPath:@”picture.data.url”],@”picture”,
[result valueForKey:@”email”],@”email”, nil];
NSLog(@”%@”,dictionaryFbSDKResponce);
//Navigate to HomeScreenViewController
}
else {
NSLog(@”%@”,[error localizedDescription]);
NSLog(@”%@”,error);
}
}];
}
}
}
– (void)loginButtonDidLogOut:(FBSDKLoginButton *)loginButton {
// After Logout button this method will fire
NSLog(@”%@”,loginButton);
}
– (BOOL) loginButtonWillLogin:(FBSDKLoginButton *)loginButton {
//Before Login and Logout button this method will fire
//Enable Or Desable Facebook Login
NSLog(@”%@”,loginButton);
return true;
}
Download Sample Project From Github