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

前端 未结 6 548
花落未央
花落未央 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 got same issue today, only because I missed one step:

    - (BOOL)application:(UIApplication *)application
                openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication
             annotation:(id)annotation {
    
        BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
                                                                      openURL:url
                                                            sourceApplication:sourceApplication
                                                                   annotation:annotation];
    
        return handled;
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-04 13:09

    You should use tokenString instead, like this:

    FBSDKAccessToken* access_token =[FBSDKAccessToken currentAccessToken].tokenString; NSLog(@"Access Token, %@",access_token);

    0 讨论(0)
  • 2021-01-04 13:11

    The FBSDKApplicationDelegate needs to be called first to resolved cached tokens. Since you are setting the root view controller immediately, that calls your viewDidLoad before the FBSDKApplicationDelegate. Instead, you can move the FBSDKApplicationDelegate up:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      // Override point for customization after application launch.
      [FBSDKLoginButton class];
      BOOL r = [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
      [self.window setRootViewController:[[RootViewController alloc] init]];
    
      [self.window makeKeyAndVisible];
    
      [self.window setBackgroundColor:[UIColor purpleColor]];
    
    
      return r;
    }
    
    0 讨论(0)
  • 2021-01-04 13:17

    You could build the logic around waiting for FBSDKAccessTokenDidChangeNotification notification in your ViewController's code.

    In your AppDelegate.m:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // ...
    
        // add observer BEFORE FBSDKApplicationDelegate's - application:didFinishLaunchingWithOptions: returns
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(fbAccessTokenDidChange:) 
                                                     name:FBSDKAccessTokenDidChangeNotification 
                                                   object:nil];
    
        return [[FBSDKApplicationDelegate sharedInstance] application: application     didFinishLaunchingWithOptions: launchOptions];
    }
    
    - (void)fbAccessTokenDidChange:(NSNotification*)notification
    {
        if ([notification.name isEqualToString:FBSDKAccessTokenDidChangeNotification]) {
            if ([FBSDKAccessToken currentAccessToken]) {
                // token is ready to be used in the app at this point
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-04 13:20

    It happened to me too, the "Login with Facebook" button was working as expected, I was being asked for permissions, allowed them, the application:openURL:... delegate method was being called with an URL that seemed valid, nonetheless the accessToken property remained nil.

    The problem in my case was the fact that somehow Shared Keychain was removed from the application entitlements, thus the Facebook SDK was not able to save the token in keychain. Enabling the capability solved the problem.

    The odd thing is that this was a silent error, so it was not clear from the beginning why the access token wasn't being set...

    0 讨论(0)
提交回复
热议问题