我正在创建一个Facebook应用程序.我已经使用FBSDKLoginManager类实现了登录.当我第一次登录Facebook时,成功块返回FBSDKLoginManagerLoginResult对象,该对象包含访问令牌对象(FBSDKAccessToken对象),该对象使用完整的方式共享包含.But在第二次(重新启动应用程序后),当我尝试获取访问令牌使用函数[FBSDKAccessToken currentAccessToken]返回nil.那么如何获得访问令牌(FBSDKAccessToken对象)第二次没有再次登录Facebook.
解决方法
尝试这段代码.基于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调用此方法后,您可以在登录后获取访问令牌
[self fetchUserInfo];