Last updated on July 31st, 2016 at 11:13 am
Facebook integration is very important feature now a days . It is seen in almost every application for direct login. Before starting Facebook integration in your iOS application , you first need to create a new application in Facebook developers account.
Step 1 : Go to developers.facebook.com and login your Facebook developers account.
- Download iOS SDK for Facebook Integration.
- Create a Facebook App on the developers account so that Facebook will know that you want to integrate Facebook in your application.
- Fill in the name of the app and choose its type . Click on create application.
Step 2 : Choose your platform
After you create Facebook app in the developers account , choose the platform to iOS.
Step 3 : Add Facebook SDK to your project .
- Drag FBSDKCoreKit and FBSDKLoginKit to your projects bundle and uncheck copy to destination folder.
- Go to yours project build settings and in the framework search paths add the path of the folder where you have downloaded the Facebook SDK. eg. /Users/Saurab-01M/Desktop/Saurabh/FacebookSDKs-iOS-20151111.
- Add your appBundleID in the Facebook web .
Step 4 : Update info.plist of your application
- Open info.plist of your application by clicking on info.plist in app bundle.
- Right click on info.plist and choose Open as >> Source code and add the code from Facebook developers website between <dict> and </dict> in your plist file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<key>CFBundleURLTypes</key> | |
<array> | |
<dict> | |
<key>CFBundleURLSchemes</key> | |
<array> | |
<string>fb578228242334769</string> | |
</array> | |
</dict> | |
</array> | |
<key>FacebookAppID</key> | |
<string>578228242334769</string> | |
<key>FacebookDisplayName</key> | |
<string>TestNew</string> | |
<key>NSAppTransportSecurity</key> | |
<dict> | |
<key>NSExceptionDomains</key> | |
<dict> | |
<key>facebook.com</key> | |
<dict> | |
<key>NSIncludesSubdomains</key> | |
<true/> | |
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key> | |
<false/> | |
</dict> | |
<key>fbcdn.net</key> | |
<dict> | |
<key>NSIncludesSubdomains</key> | |
<true/> | |
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key> | |
<false/> | |
</dict> | |
<key>akamaihd.net</key> | |
<dict> | |
<key>NSIncludesSubdomains</key> | |
<true/> | |
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key> | |
<false/> | |
</dict> | |
</dict> | |
</dict> | |
<key>LSApplicationQueriesSchemes</key> | |
<array> | |
<string>fbapi</string> | |
<string>fb-messenger-api</string> | |
<string>fbauth2</string> | |
<string>fbshareextension</string> | |
</array> |
This will add the Facebook`s app id and Transport settings to your application.
Step 5 : Make changes in the appDelegate.m to connect the Facebook to your application
1) Import FBSDKCoreKit/FBSDKCoreKit.h in appDelegate.h .
2) Add the Facebook Delegate in the DidFinish launching with options of AppDelegate.m as
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { | |
// Override point for customization after application launch. | |
[[FBSDKApplicationDelegate sharedInstance] application:application | |
didFinishLaunchingWithOptions:launchOptions]; | |
return YES; | |
} |
3) After didFinishLauching , add the following code to the appDelegate.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
– (void)applicationDidBecomeActive:(UIApplication *)application { | |
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. | |
[FBSDKAppEvents activateApp]; | |
} | |
– (BOOL)application:(UIApplication *)application | |
openURL:(NSURL *)url | |
sourceApplication:(NSString *)sourceApplication | |
annotation:(id)annotation { | |
return [[FBSDKApplicationDelegate sharedInstance] application:application | |
openURL:url | |
sourceApplication:sourceApplication | |
annotation:annotation]; | |
} |
Step 6 : Import the frameWorks in ViewController .m file as
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h>
Now , update your ViewController.m by adding following code to add a login button and access name , email and access token through Facebook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface ViewController () | |
{ | |
NSString *name; | |
NSString *email ; | |
NSString *acessToken; | |
} | |
@end | |
@implementation ViewController | |
– (void)viewDidLoad { | |
[super viewDidLoad]; | |
} | |
– (void)viewWillAppear:(BOOL)animated | |
{ | |
[super viewWillAppear:YES]; | |
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 50)]; | |
nameLabel.text = name; | |
nameLabel.textColor = [UIColor blackColor]; | |
[self.view addSubview:nameLabel]; | |
UILabel *emailLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 150, 200, 50)]; | |
emailLabel.text = email; | |
[self.view addSubview:emailLabel]; | |
UILabel *accessTokenLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 200, 50)]; | |
accessTokenLabel.text = acessToken; | |
[self.view addSubview:accessTokenLabel]; | |
UIButton *myLoginButton=[UIButton buttonWithType:UIButtonTypeCustom]; | |
myLoginButton.backgroundColor=[UIColor darkGrayColor]; | |
myLoginButton.frame=CGRectMake(0,0,300,40); | |
myLoginButton.center = self.view.center; | |
[myLoginButton setTitle: @"My Login Button" forState: UIControlStateNormal]; | |
[myLoginButton addTarget:self | |
action:@selector(loginButtonClicked) forControlEvents:UIControlEventTouchUpInside]; | |
[self.view addSubview:myLoginButton]; | |
} | |
-(void)loginButtonClicked | |
{ | |
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; | |
[login | |
logInWithReadPermissions: @[@"public_profile",@"user_friends",@"email"] | |
fromViewController:self | |
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { | |
if (error) { | |
NSLog(@"Process error"); | |
} else if (result.isCancelled) { | |
NSLog(@"Cancelled"); | |
} else { | |
NSLog(@"Logged in"); | |
NSLog(@"Result %@",result); | |
} | |
}]; | |
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] | |
initWithGraphPath:@"/me" | |
parameters:@{ @"fields": @"id,name,email"} | |
HTTPMethod:@"GET"]; | |
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { | |
NSLog(@"resds %@",result); | |
name = [result valueForKey:@"name"]; | |
email = [result valueForKey:@"email"]; | |
acessToken = [result valueForKey:@"id"]; | |
}]; | |
} |
Output
Facebook integration is completed . For more blogs related iOS development visit codeObjectiveC.blogspot
Thank you 🙂