ios facebook sdk 4.0 login error code 304

jim picture jim · Apr 2, 2015 · Viewed 27.7k times · Source

I've just updated facebook sdk v4.0

and according the tutorial of Using Custom Login UIs

-(IBAction)facebookLoginClick:(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 you ask for multiple permissions at once, you
        // should check if specific permissions missing
        if ([result.grantedPermissions containsObject:@"email"]) {
            // Do work
        }
    }
}];
}

BUT the result is always nil and error code is 304, am I missing something?

Answer

Vineeth Joseph picture Vineeth Joseph · May 22, 2015

I had a similar problem.

After initialising FBSDKLoginManager I added a line to flush out the data and the (Facebook)Token:

FBSDKLoginManager *loginmanager= [[FBSDKLoginManager alloc]init];    
[loginmanager logOut];

Hope this helps.


Thus, exactly as the OP asks, "am I missing something"?

Yes, the following standard example code which is seen everywhere, is simply wrong:

-(IBAction)facebookLoginClick:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
---- ONE MAGIC LINE OF CODE IS MISSING HERE ----
[login logInWithReadPermissions:@[@"email"]
   handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
    {
    if (error) {...}
    else if (result.isCancelled) {...}
    else { // (NB for multiple permissions, check every one)
       if ([result.grantedPermissions containsObject:@"email"])
          { NSLog(@"%@",result.token); }
       }
}];
}

you must do this:

-(IBAction)facebookLoginClick:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logOut];   //ESSENTIAL LINE OF CODE
[login logInWithReadPermissions:@[@"email"]
   handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
    {
    if (error) {...}
    else if (result.isCancelled) {...}
    else { // (NB for multiple permissions, check every one)
       if ([result.grantedPermissions containsObject:@"email"])
          { NSLog(@"%@",result.token); }
       }
}];
}

Otherwise, very simply, the app will not work if the user happens to change FB accounts on the device. (Unless they happen to for some reason re-install the app!)

Once again - the popular sample code above simply does not work (the app goes in to an endless loop) if a user happens to change FB accounts. The logOut call must be made.