Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to get the access token through [FBSDKAccessToken currentAccessToken] in iphone sdk

I am creating a facebook app. I have implemented the login using class FBSDKLoginManager. At first time when i login the facebook, success block return the FBSDKLoginManagerLoginResult object which contain the access token object (FBSDKAccessToken object) which is use full for share the contain .But at second time (after relaunching the app) when I try to get the access token using the function [FBSDKAccessToken currentAccessToken] return nil. So how to get the access token (FBSDKAccessToken object) second time without login facebook again.

like image 234
user2102502 Avatar asked May 16 '15 10:05

user2102502


4 Answers

Try This code. Based on Facebook SDK version 4.0

AppDalegate.m

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

ViewController.m

- (IBAction)btnFacebookPressed:(id)sender {
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
     {
         if (error)
         {
             // Process error
         }
         else if (result.isCancelled)
         {
             // Handle cancellations
         }
         else
         {
             if ([result.grantedPermissions containsObject:@"email"])
             {
                 NSLog(@"result is:%@",result);
                 [self fetchUserInfo];
             }
         }
     }];
}

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

        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {
                 NSLog(@"resultis:%@",result);
             }
             else
             {
                 NSLog(@"Error %@",error);
             }
         }];

    }

}

viewDidLoad call this method you get access token after login

[self fetchUserInfo];
like image 88
Dharmesh Dhorajiya Avatar answered Nov 03 '22 21:11

Dharmesh Dhorajiya


You need to call

[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]

Before

[FBSDKAccessToken currentAccessToken]
like image 19
AlexeyVMP Avatar answered Nov 03 '22 20:11

AlexeyVMP


Try This code. Based on Facebook SDK version 6.0 and above

- (IBAction)fbIntegration:(id)sender
{

    FBSDKLoginManager *loginmanager = [[FBSDKLoginManager alloc] init];

    [loginmanager logInWithReadPermissions:@[@"email",@"public_profile"] fromViewController:Nil handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)

     {
         if (error)
         {
             // Process error
             NSLog(@"======error%@",error);
         }
         else if (result.isCancelled)
         {
             // Handle cancellations
             NSLog(@"canceled");
         }
         else
         {
             if ([result.grantedPermissions containsObject:@"email"])
             {
                 NSLog(@"result is:%@",result);
                 [self fetchUserInfo];
             }
         }
     }];

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

        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {
                 NSLog(@"%@",connection);
              }
             else
             {
                 NSLog(@"Error %@",error);
             }
         }];

    }

}

hope this will helps to others.

like image 3
Kishore Kumar Avatar answered Nov 03 '22 20:11

Kishore Kumar


If you are trying the your application on simulator, please try on a real device because simulator has a user defaults bug.

Falling back to loading access token from NSUserDefaults because of simulator bug

like image 1
mhmtkrgz Avatar answered Nov 03 '22 22:11

mhmtkrgz