ios Facebook SDK v4.x access token null despite being logged in via FBSDKLoginButton

前端 未结 6 547
花落未央
花落未央 2021-01-04 12:34

My facebook access token is null despite the fact that the button shows that I\'m logged in. Anyone know why this would be?

From RootViewController.m



        
6条回答
  •  孤街浪徒
    2021-01-04 12:53

    I have had an issue where I could not get the Access Token because I am calling it before return [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];. I needed to check the access token to check whether the user is logged in or not to decide which should be my root view controller. What I did was I tried to get the access token from the cache manually in application didFinishLaunchingWithOptions by:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        FBSDKProfile *cachedProfile = [FBSDKProfile fetchCachedProfile];
        [FBSDKProfile setCurrentProfile:cachedProfile];
    
        FBSDKAccessToken *cachedToken = [[FBSDKSettings accessTokenCache] fetchAccessToken];
        NSLog(@"Cached Token: %@", cachedToken.tokenString);
    
        if (cachedToken) {
            //User is logged in, do logic here.
        }
    
        return [[FBSDKApplicationDelegate sharedInstance] application:application
                                    didFinishLaunchingWithOptions:launchOptions];
    }
    

    You can see this method by checking the implementation of - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions in FBSDKApplicationDelegate.m

    UPDATE

    This does not seem to work anymore. I'm not sure why. But it seems that the fetchCachedProfile and accessTokenCache methods have been made internal. I could not access these methods anymore in my code as it gives me an error.

提交回复
热议问题