Wednesday, 17 February 2016

how to login Facebook Ios 9 iphone

AppDelegate.m file

    #import <FBSDKLoginKit/FBSDKLoginKit.h>
    #import <FBSDKCoreKit/FBSDKCoreKit.h>

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
  return [[FBSDKApplicationDelegate sharedInstance]
application:application   openURL:url
   sourceApplication:sourceApplication
                      annotation:annotation];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
  [FBSDKAppEvents activateApp];
}

- (BOOL)application:(UIApplication *)application
      didFinishLaunchingWithOptions:

(NSDictionary *)launchOptions {
  return [[FBSDKApplicationDelegate sharedInstance]
application:application didFinishLaunchingWithOptions:launchOptions];



 ==>Login ViewController.m


-(IBAction)facebookLogin:(id)sender
{
  FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

   if ([FBSDKAccessToken currentAccessToken])
   {
     NSLog(@"Token is available : %@",

[[FBSDKAccessToken currentAccessToken]tokenString]);
        [self fetchUserInfo];
    }
    else
    {
 [login logInWithReadPermissions:@[@"email"] fromViewController:

 self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
         {
             if (error)
             {
                 NSLog(@"Login process error");
             }
             else if (result.isCancelled)
             {
                 NSLog(@"User cancelled login");
             }
             else
             {
                 NSLog(@"Login Success");

       if ([result.grantedPermissions containsObject:@"email"])
           {
          NSLog(@"result is:%@",result);
        [self fetchUserInfo];
          }
        else
           {
 [SVProgressHUD showErrorWithStatus:@"Facebook email permission error"];

           }
             }
         }];
    }
}

-(void)fetchUserInfo
{
    if ([FBSDKAccessToken currentAccessToken])
    {
        NSLog(@"Token is available : %@",

[[FBSDKAccessToken currentAccessToken]tokenString]);

        [[[FBSDKGraphRequest alloc] initWithGraphPath:

@"me" parameters:@{@"fields": @"id, name, email"}]
         startWithCompletionHandler:

^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {
                 NSLog(@"results:%@",result);

                 NSString *email = [result objectForKey:@"email"];
                 NSString *userId = [result objectForKey:@"id"];

                 if (email.length >0 )
                 {
                     //Start you app Todo
                 }
                 else
                 {
                    NSLog(@“Facebook email is not verified");
                 }
             }
             else
             {
                 NSLog(@"Error %@",error);
            }
         }];
    }
}