无法通过iphone sdk中的[FBSDKAccessToken currentAccessToken]获取访问令牌 [英] unable to get the access token through [FBSDKAccessToken currentAccessToken] in iphone sdk

查看:551
本文介绍了无法通过iphone sdk中的[FBSDKAccessToken currentAccessToken]获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Facebook应用程序。我已经使用FBSDKLoginManager类实现了登录。当我第一次登录Facebook时,成功块返回包含访问令牌对象(FBSDKAccessToken对象) FBSDKLoginManagerLoginResult 对象,该对象用于共享包含的。第二次(重新启动应用程序后)当我尝试使用函数 [FBSDKAccessToken currentAccessToken] 返回nil获取访问令牌。那么如何第二次获取访问令牌(FBSDKAccessToken对象),而不再登录Facebook。

解决方案

尝试这段代码。基于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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆