简体   繁体   English

如何以iOS最好的方式获取Facebook的accessToken?

[英]how to get facebook accessToken in ios best way?

I tried below code, but it is working with mistakes. 我尝试了下面的代码,但是它在处理错误。

- (void) openActiveSessionWithPermissions: (NSArray *) arr
{
    if ([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded){
        [self openWithUI:NO permissions:arr];
    } else if ([FBSession activeSession].state == FBSessionStateOpen || [FBSession activeSession].state == FBSessionStateOpenTokenExtended){
        [[FBSession activeSession] closeAndClearTokenInformation];
    } else {
        [[FBSession activeSession] closeAndClearTokenInformation];
        [self openWithUI:YES permissions:arr];
    }
}

- (void) openWithUI: (BOOL) UI permissions: (NSArray *) permissions
{
    [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:UI completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {

        if (error) {
            [self.delegate didReceieveError:[error description]];
        } else {
            [self.delegate didTakeAccessToken:session.accessTokenData.accessToken];
        }
    }];
}

I could not find my mistakes. 我找不到我的错误。 What is the best way to get accesToken ? 获得accesToken的最佳方法是什么? Thanks. 谢谢。

Try this : 尝试这个 :

 [FBSession openActiveSessionWithReadPermissions:@[@"basic_info", @"email"]
                                           allowLoginUI:YES
                                      completionHandler:
         ^(FBSession *session, FBSessionState state, NSError *error)
         {
             if (FBSession.activeSession.isOpen)
             {
                 [[FBRequest requestForMe] startWithCompletionHandler:
                  ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error)
                  {
                      if (!error)
                      {
                          //Get AccessToken                              
                          NSString *aStrFBAccessToken = [[[FBSession activeSession] accessTokenData] accessToken];              
                      }
                  }];
             }
         }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM