Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
679 views
in Technique[技术] by (71.8m points)

objective c - ios facebook sdk 4.0 login error code 304

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...