简体   繁体   中英

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 ? 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];              
                      }
                  }];
             }
         }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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