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

前端 未结 4 1776
野性不改
野性不改 2020-11-27 22:13

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 FBSDKLogi

4条回答
  •  粉色の甜心
    2020-11-27 22:57

    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];
    

提交回复
热议问题