How to restart an app when resumes from multi-task

2019-04-16 08:22发布

问题:

So my app relies on an internet connection and every time the app is opened from the home screen it checks using Reachability and then allows the user to continue or tells them to get connected. My issue however is that when the app is resumed from multitasking, it skips the initial view controller and goes straight to where they were. Is there any way in swift that I could get the app to refresh or restart when it is resumed from multi-tasking?

Thanks

EDIT When using this code:

let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
self.window.rootViewController = MainStoryboard.instantiateInitialViewController()

I get expected declaration error.

回答1:

You can set your root view controller and it will restart you main VC. Assuming your using a storyboard:

(self is referring to your AppDelegate)

Swift:

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
self.window.rootViewController = storyboard.instantiateInitialViewController()

Objective-C:

self.window.rootViewController = [[UIStoryboard storyboardWithName:@"MyStoryboardName" bundle:nil] instantiateInitialViewController];


回答2:

I just added the following in the plist: "Application does not run in background" and set it to "YES"

Seem to be working.



回答3:

You can use navigation controller:

    let vc = self.storyboard!.instantiateViewController(withIdentifier: "FirstPageViewController") as! FirstPageViewController
    self.navigationController?.pushViewController(vc, animated: true)