简体   繁体   中英

Facebook SDK on iOS App Session Fixing

I'm trying to authenticate users through facebook. I've read the SDK and done every necessary things that need to be done, BUT if I try to Login into my app while LoggedIn in this place, 本地Facebook登录

It Tells me SESSION CLOSED but if I logged out and try to SIgnIn into my app, it checks if I've the facebook app and try to check if logged in if I'm, it updates and do what I want it to do, but if I'm not, it brings a Dialog for me to login. But, once I sign into the Image above, the other thing happens CLOSED SESSION ERROR . Would appreciate the help, thanks.

My Code Below

    //For the button
      - (IBAction)facebook:(id)sender {

// If the session state is any of the two "open" states when the button is clicked
if (FBSession.activeSession.state == FBSessionStateOpen || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {
    [FBSession.activeSession closeAndClearTokenInformation];
} else {
[FBSession openActiveSessionWithReadPermissions:@[@"public_profile",@"user_friends"]
                                   allowLoginUI:YES
                              completionHandler: ^(FBSession *session, FBSessionState state, NSError *error) {
                                  // Retrieve the app delegate
                                  appDelegate = [UIApplication sharedApplication].delegate;
                                  // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
                                  [appDelegate sessionStateChanged:session state:state error:error];

                                  if (state == FBSessionStateOpen) {
                                      [self fetchUserDetails];
                                      UINavigationController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainViewController"];
                                      [self presentViewController:vc animated:YES completion:nil];
                                  }
                              }];

}
}

//AppDelegate
   - (BOOL)application:(UIApplication *)application
          openURL:(NSURL *)url
        sourceApplication:(NSString *)sourceApplication
        annotation:(id)annotation
 {
  //return [FBAppCall handleOpenURL:url sourceApplication:annotation];
  return [FBSession.activeSession handleOpenURL:url];
 }

  - (void)applicationDidBecomeActive:(UIApplication *)application
  {

   // Handle the user leaving the app while the Facebook login dialog is being shown
   // For example: when the user presses the iOS "home" button while the login dialog is    active
    [FBAppCall handleDidBecomeActive];
   }

   // This method will handle ALL the session state changes in the app
        - (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
 {
     // If the session was opened successfully
if (!error && state == FBSessionStateOpen){
    NSLog(@"Session opened");
    // Show the user the logged-in UI
    return;
}
if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
    // If the session is closed
    NSLog(@"Session closed");
    // Show the user the logged-out UI

}

// Handle errors
if (error){
    NSLog(@"Error");
    NSString *alertText;
    NSString *alertTitle;
    // If the error requires people using an app to make an action outside of the app in order to recover
    if ([FBErrorUtility shouldNotifyUserForError:error] == YES){
        alertTitle = @"Something went wrong";
        alertText = [FBErrorUtility userMessageForError:error];
        //[self showMessage:alertText withTitle:alertTitle];
        NSLog(@"%@", alertText);
    } else {

        // If the user cancelled login, do nothing
        if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
            NSLog(@"User cancelled login");

            // Handle session closures that happen outside of the app
        } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession){
            alertTitle = @"Session Error";
            alertText = @"Your current session is no longer valid. Please log in again.";
            //[self showMessage:alertText withTitle:alertTitle];
            NSLog(@"%@", alertText);

            // Here we will handle all other errors with a generic error message.
            // We recommend you check our Handling Errors guide for more information
            // https://developers.facebook.com/docs/ios/errors/
        } else {
            //Get more error information from the error
            NSDictionary *errorInformation = [[[error.userInfo objectForKey:@"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:@"body"] objectForKey:@"error"];

            // Show the user an error message
            alertTitle = @"Something went wrong";
            alertText = [NSString stringWithFormat:@"Please retry. \n\n If the problem persists contact us and mention this error code: %@", [errorInformation objectForKey:@"message"]];
            //[self showMessage:alertText withTitle:alertTitle];
            NSLog(@"%@", alertText);
        }
    }
    // Clear this token
    [FBSession.activeSession closeAndClearTokenInformation];
    // Show the user the logged-out UI

}

}

在此处输入图片说明

I add a similar problem, in my case I found the reason, I was using an application id that was not correct (because of test environment/production environment configuration of my app, I was developing and I had no facebook app for the development application id).

I'm sharing my approach for debugging the problem:

  • just launch the app from xcode, put a break point where the error is intercepted (CordovaFacebook.m) and there if you are lucky you should see a better description of the problem

Furthermore, remember to set the correct fb app id inside .plist remember also that if you are using a test application on facebook, that application should be either publicly available or you should set some test users inside the ROLES tab of the admin section

Another source of interesting debugging informations is the following:

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    if (!url) {
        return NO;
    }

在此处输入图片说明

set a break point there and inspect the url variable content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM