无法通过iphone sdk中的[FBSDKAccessToken currentAccessToken]获取访问令牌 [英] unable to get the access token through [FBSDKAccessToken currentAccessToken] in iphone sdk
问题描述
尝试这段代码。基于Facebook SDK 4.0版
AppDalegate.m
code> - (BOOL)应用程序:(UIApplication *)应用程序openURL:(NSURL *)url sourceApplication :( NSString *)sourceApplication注释:(id)注释{
return [[FBSDKApplicationDelegate sharedInstance]
openURL:url
sourceApplication:sourceApplication
注释:注释];
}
ViewController.m
- (IBAction)btnFacebookPressed:(id)sender {
FBSDKLoginManager * login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@ [@email] handler:^(FBSDKLoginManagerLoginResult * result,NSError * error)
{
if(error)
{
/ /进程错误
}
else if(result.isCancelled)
{
//处理取消
}
else
{
if([result.grantedPermissions containsObject:@email])
{
NSLog(@result is:%@,result);
[self fetchUserInfo];
}
}
}];
- (void)fetchUserInfo
{
if([FBSDKAccessToken currentAccessToken])
{
NSLog(@令牌可用:%@,[[FBSDKAccessToken currentAccessToken] tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:@me参数:@ {@fields:@id,name,link,first_name,last_name,picture.type(large)生活,生物,位置,朋友,家乡,朋友列表}
startWithCompletionHandler:^(FBSDKGraphRequestConnection * connection,id result,NSError * error){
if(!error)
{
NSLog(@resultis:%@,result);
}
else
{
NSLog(@Error%@,error);
}
}];
}
}
viewDidLoad
在登录后调用此方法获取访问令牌
[self fetchUserInfo];
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.
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];
这篇关于无法通过iphone sdk中的[FBSDKAccessToken currentAccessToken]获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!