Skip to content

Instantly share code, notes, and snippets.

@randhirraj3130
Created April 5, 2017 06:14
Show Gist options
  • Save randhirraj3130/ae917637df9715df06b7768081ac755f to your computer and use it in GitHub Desktop.
Save randhirraj3130/ae917637df9715df06b7768081ac755f to your computer and use it in GitHub Desktop.
facebook login in objective c
// add the following code in to info.plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb1801381186770425</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>AddYourID</string>
<key>FacebookDisplayName</key>
<string>YourAppName</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
<string>instagram</string>
</array>
<key>NSLocationWhenInUseUsageDescription</key>
<string>To access your current location</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>To share </string>
// add the following code in to appdelegate.m
#import "LoginViewController.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
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;
}
// call the following code into header
// change the header button type to fbsdklogin button type
// call the following function into view controller
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
-(void)faceBookLogin{
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithReadPermissions:@[@"public_profile", @"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
//TODO: process error or result.
if (error) {
NSLog(@"Process error");
[self facebookLoginFailed:YES];
} else if (result.isCancelled) {
[self facebookLoginFailed:NO];
} else {
NSLog(@"working");
if ([FBSDKAccessToken currentAccessToken]) {
NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
[parameters setValue:@"id,name,email,first_name,last_name,picture.type(large)" forKey:@"fields"];
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, NSDictionary* result, NSError *error) {
if (!error) {
NSLog(@"working");
NSLog(@"%@",result);
}
}];
}
else {
[self facebookLoginFailed:YES];
}
}
}];
}
- (void)facebookLoginFailed:(BOOL)isFBResponce{
if(isFBResponce){
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Failed", nil) message:NSLocalizedString(@"request_error", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"pop_ok", nil) otherButtonTitles: nil];
[alert show];
}
else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Failed", nil) message:NSLocalizedString(@"loginfb_cancelled", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"pop_ok", nil) otherButtonTitles: nil];
[alert show];
}
}
@property (weak, nonatomic) IBOutlet FBSDKLoginButton *FbloginBtn1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment