Facebook signin ios

Follow the official document procedure
https://developers.facebook.com/docs/facebook-login/ios

1.Download SDK and setup the environment
https://developers.facebook.com/docs/ios/getting-started

Create new Facebook app
1.FB app 2 of 2

1.FB app 1 of 2

2.Configure the XCode project parameters
Info.plist

2.InfoPlist

For AppTransportSecurity parameters, you can follow the Facebook direction, or you can just “allow all” as follow

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

3.Modify the source code

3.1 AppDelegate
#import <FBSDKCoreKit/FBSDKCoreKit.h>

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
….
}

3.2 ViewController you want to do Facebook Signin
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>

-(void)loginAsFacebook {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:  @[@”public_profile”, @”email”, @”user_friends”] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(@”Process error”);
} else if (result.isCancelled) {
NSLog(@”Cancelled”);
} else {
NSLog(@”Logged in”);

NSLog(@”[%@][%@][%@][%@]”, [[FBSDKAccessToken currentAccessToken]appID],
[[FBSDKAccessToken currentAccessToken]userID],
[[FBSDKAccessToken currentAccessToken]refreshDate],
[[FBSDKAccessToken currentAccessToken]tokenString]
);

//Get some profile data
FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@”me” parameters:@{@”fields”: @”email, first_name, last_name”}];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if(result) {
NSString *sUserID, *sEmail, *sFirstName, *sLastName;
if ([result objectForKey:@”email”]) {
sEmail = [result objectForKey:@”email”];
}
if ([result objectForKey:@”first_name”]) {
sFirstName = [result objectForKey:@”first_name”];
}
if ([result objectForKey:@”last_name”]) {
sLastName = [result objectForKey:@”last_name”];
}
if ([result objectForKey:@”id”]) {
sUserID = [result objectForKey:@”id”];
}

NSLog(@”Customer details:[%@][%@][%@][%@]”, sUserID, sFirstName, sLastName, sEmail);

//Facebook account was authenticated OK, do whatever you need
}];
[connection start];
}
}];
}

 

By admin

Leave a Reply