4

Using Facebook iOS SDK 3.1.

When choosing not to allow a 'Connect' to a Facebook app, I'm stuck with Facebook throwing "com.facebook.sdk error2." errors at me, even after re-installing my app.

Steps to reproduce:

  1. Choose to connect with Facebook
  2. Select 'Not Now' in the UIAlertView that pops up

=> I can't choose to connect again.

The only way for the user to connect again is to remove her Facebook account from Settings and add it again.

Is this a bug in the Facebook SDK or am I missing something?

I've obviously followed the authorization tutorial and everything works just fine (auth, posting stuff) when choosing to connect.

Kristofer Sommestad
  • 3,061
  • 27
  • 39

2 Answers2

10

OK, so I figured out what goes on here. When declining authorization of the app, this is stored as a setting on your device's Facebook account (Settings > Facebook).

By going to Settings and re-enabling the app in question, you can try to connect again. Not very clear to users, but you can catch this error and show some kind of info to the user.

This is how I implemented it (compared to Facebook's default error handling):

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState)state
                      error:(NSError *)error {
....

if (error) {
        NSString *errorTitle = NSLocalizedString(@"Error", @"Facebook connect");
        NSString *errorMessage = [error localizedDescription];
        if (error.code == FBErrorLoginFailedOrCancelled) {
            errorTitle = NSLocalizedString(@"Facebook Login Failed", @"Facebook Connect");
            errorMessage = NSLocalizedString(@"Make sure you've allowed My App to use Facebook in Settings > Facebook.", @"Facebook connect");
        }

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:errorTitle
                                                            message:errorMessage
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"OK", @"Facebook Connect")
                                                  otherButtonTitles:nil];
        [alertView show];
    }

}

Kristofer Sommestad
  • 3,061
  • 27
  • 39
  • Did you also check what happens if you de-authorize your Facebook app via the Facebook privacy settings on facebook.com? Do you get the same error or a different one and how does it affect the Facebook settings on the device? – borisdiakur Sep 27 '12 at 13:36
  • No, but looking at the error code alone I'm guessing it'll be the same error. – Kristofer Sommestad Sep 27 '12 at 14:10
  • +1. Good solution. Facebook's default error message should be more descriptive like yours. – hughesdan Sep 29 '12 at 21:38
  • 2
    There is a recent answer on that issue by Jason Clark (a facebook engineer it seems) providing a way to solve the problem without redirecting the user to the device settings. Here is the thread: http://stackoverflow.com/questions/12601191/facebook-sdk-3-1-error-validating-access-token – borisdiakur Sep 29 '12 at 21:44
  • may be a n00b question, but where did you add in this error handling? – ohayon Jan 30 '13 at 16:16
  • @ohwutup In the `-sessionStateChanged:state:error` method. See step 4 ("Implement the login flow") in Facebook's tutorial for more details: https://developers.facebook.com/docs/howtos/login-with-facebook-using-ios-sdk/. – Kristofer Sommestad Jan 31 '13 at 12:43
  • @KristoferSommestad In settings - my app is ON stage. But its still show this error. – sathiamoorthy Feb 20 '13 at 11:15
  • @sathiamoorthy Yeah, I've seen that too. It seems to behave oddly sometiems; I've had to toggle it between on-off-on to make it work. And on some occasion, I even had to reinstall the app to make it work. – Kristofer Sommestad Feb 20 '13 at 13:53
  • @KristoferSommestad I should reinstall the app. is this the only way to overcome this problem?. – sathiamoorthy Feb 21 '13 at 03:48
  • @sathiamoorthy For me, that always resolved the issue. However, I think it was occasionally resolved by toggling the on/off switch back and forth as well, but I can't remember if this always helped (or if it was the reinstall that actually did the trick)... – Kristofer Sommestad Feb 21 '13 at 10:08
1

I had the same issue when I was trying to log in on Facebook using Facebook framework when an account is added in the setting but I fixed this by using the following code:

as much as I know "com.facebook.sdk error2." comes when there is an account is added in the iphone.

appdelegate.h

@property (strong, nonatomic) FBSession *mysession;

just add the method in the appdelegate.m

-(void)openSessionWithAllowLoginUI:(BOOL)allowLoginUI{

    if (!self.mysession.isOpen) {
        // create a fresh session object
        self.mysession = [[FBSession alloc] initWithPermissions:permissions];
    }

        [self.mysession openWithCompletionHandler:^(FBSession *session,
                                                    FBSessionState stat,
                                                    NSError *error){
            [self sessionStateChanged:session
                                state:stat
                                error:error];
         NSLog(@"Session Staet is = %u",stat);

            switch (stat){
                case FBSessionStateClosed:
                    break;
                case FBSessionStateOpen:{
                        NSString *strAccessToken1 = [mysession accessToken];
                        NSLog(@"AccessToken = %@",strAccessToken1);
                        NSString  *urlstring1 = [NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",strAccessToken1];

                        NSURL *url1 = [NSURL URLWithString:[urlstring1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                        NSString  *jsonRes = [NSString stringWithContentsOfURL:url1 encoding:NSUTF8StringEncoding error:nil];
                        NSDictionary *facebookData = [jsonRes JSONValue];
                        NSLog(@"FBSessionStateOpen = %@",facebookData);

                        NSString *strFBID = [[NSString alloc]initWithString:[NSString stringWithFormat:@"%@",[facebookData objectForKey:@"id"]]];

                        NSString *strName = [[NSString alloc]initWithString:[facebookData objectForKey:@"name"]];

                        NSLog(@"FBSessionStateOpen = %@",strName);

                        NSString *ProfileImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture",strFBID];

                        NSLog(@"Profile image URL is = %@",ProfileImageURL);

                        NSString *strImageURl = [[NSString alloc]initWithString:ProfileImageURL];

                        strAccessToken = [[NSString alloc]initWithString:strAccessToken1];
                        strAppUserName = [[NSString alloc]initWithString:strName];
                        strFacebookUsername = [[NSString alloc]initWithString:strName];
                        strAppUserProfileImage = [[NSString alloc]initWithString:strImageURl];
                        strFacebookUserId = [[NSString alloc]initWithString:strFBID];

                    break;
                }
                default:
                    break;
            }
      }];
}

this in my action method

-(IBAction)Facebook_Btn_Clicked:(id)sender{
        [appDelegate openSessionWithAllowLoginUI:YES];

      }
Vinod Singh
  • 1,374
  • 14
  • 25