Choosing 'Not Now' when accepting app causes “com.facebook.sdk error2”

℡╲_俬逩灬. 提交于 2019-11-30 10:10:29

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];
    }

}

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];

      }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!