无法通过iPhone SDK中的获取访问令牌

我正在创build一个Facebook应用程序。 我已经使用FBSDKLoginManager类实现了login。 当我第一次loginFacebook的时候,成功块返回包含访问令牌对象(FBSDKAccessToken对象)FBSDKLoginManagerLoginResult对象,这个对象被用来共享包含的内容。但是当我尝试获取这个对象的时候(在重新启动应用程序之后)访问令牌使用函数[FBSDKAccessToken currentAccessToken] return nil。 那么如何再次获得访问令牌(FBSDKAccessToken对象)没有loginFacebook的第二次。

试试这个代码。 基于Facebook SDK版本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在login后调用这个方法获得访问令牌

 [self fetchUserInfo]; 

你需要打电话

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

之前

 [FBSDKAccessToken currentAccessToken] 

试试这个代码。 基于Facebook SDK 6.0及以上版本

 - (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); } }]; } } 

希望这会帮助别人。

如果你正在模拟器上尝试你的应用程序,请尝试一个真实的设备,因为模拟器有一个用户默认错误。

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