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

你。 提交于 2019-12-18 13:37:39

问题


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.


回答1:


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

}




回答2:


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

      }


来源:https://stackoverflow.com/questions/12620760/choosing-not-now-when-accepting-app-causes-com-facebook-sdk-error2

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