简体   繁体   中英

dismiss viewcontroller in ios

this is the code for my facebook login, when i finish the login the FaceBookLogIn.xib don't dismiss properly,

when i exit the app and relaunch it work fine and my main view appear.

how can i dismiss the viewcontroller as soon as the user singin ?

update

i move all the code from app AppDelegate to my main view controller

and when i sing in i get in the nslog

"2013-12-07 13:02:49.650 barbar[2275:70b] User session found"

bat still the view don't dismiss

if i exit and return to the app it show the tabbarviewcontroler and i get my nslog -

2013-12-07 13:06:42.162 barbar[2303:70b] User Logged Innnnn

   - (void)viewDidLoad
{
    [super viewDidLoad];

}

-(void)viewDidAppear:(BOOL)animated{


    if (FBSession.activeSession.state
        == FBSessionStateCreatedTokenLoaded) {

        NSLog(@"User Logged Innnnn");
        //[self dismissViewControllerAnimated:YES completion:Nil];
    }
    //create and present the login view controller
    else {
        NSLog(@"the user not login");
        FacebookConect * showfacebookconnectview = [[FacebookConect alloc]initWithNibName:@"FacebookConect" bundle:Nil];
        [self presentViewController:showfacebookconnectview animated:YES completion:Nil];


    }
    animated = YES;
}

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                // We have a valid session
                NSLog(@"User session found");
                [self dismissViewControllerAnimated:YES completion:Nil];
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }

    [[NSNotificationCenter defaultCenter]
     postNotificationName:FBSessionStateChangedNotification
     object:session];

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
    /*
     we pass this permissions array into our request
     I only request email, but there are many more options
     */
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"email",
                            nil];

    return [FBSession
            openActiveSessionWithReadPermissions:permissions
            allowLoginUI:allowLoginUI
            completionHandler:^(FBSession *session,
                                FBSessionState state,
                                NSError *error) {
                [self sessionStateChanged:session
                                    state:state
                                    error:error];

            }];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    // attempt to extract a token from the url
    return [FBSession.activeSession handleOpenURL:url];
}



@end

I wouldn't recommend using the AppDelegate to show login screens directly. It looks as though you are using Storyboards/Xib's to show your interface. For this reason, I'd allow the default operation to happen (because you're preventing it by setting the rootViewController property manually). Then, in your main view controller, have the check for Facebook that would bring up the dialog ( presentViewController:animated:completion: ) and can then be dismissed as above and your original main view controller will be shown.

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