Question

I am writing a turn-based game for the iOS platform. The client communicates with a remote server using the CocoaAsyncSocket API. Right now I work on this case: the client has been inactive for a while, and has been disconnected from the server due to timeout. If that's the case, I wish to pop back to the login view when the app enters the foreground, to let the user log back in again.

I assume I have to do this kind of work in the -(void)applicationWillEnterForeground of my app's delegate. Checking whether I'm connected or not is not a problem, but I don't know how to dismiss every presenting view controller to take me back to the root view (which happens to be the login view).

My view controllers are presented modally, but I also have two navigation controllers with table views including push segues.

Any help with this problem is highly appreciated. Thanks in advance!

Was it helpful?

Solution

The jarring way to do it is just replace the rootViewController with a new login view controller.

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    if ([self isDisconnected]) {
        self.window.rootViewController = [MyLoginController new];
    }
}

For storyboards, assuming your initial storyboard is the login storyboard.

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    if ([self isDisconnected]) {
        UIStoryboard *storyboard = self.window.rootViewController.storyboard
        self.window.rootViewController = [storyboard instantiateInitialViewController];
    }
}

Depending on your UX, this may or may not be appropriate.

OTHER TIPS

If all of your content views are presented modally from the root view controller (which doesn't sound like a great idea) then from the app delegate (indeed in applicationWillEnterForeground),

// if we need to login
UIViewController *rootViewController = self.window.rootViewController;
[rootViewController dismissViewControllerAnimated:YES completion:nil];

If the root view controller isn't presenting modally then you need to dismiss in the appropriate way. If any other 'child' view controller is presenting modally then it should dismiss. I guess dismissing when that view controller changes parent view controller will work. You may need to add a notification to inform all view controllers that everything is being torn down.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top