How to restart an app when resumes from multi-task

2019-04-16 08:26发布

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. enter image description here

3条回答
女痞
2楼-- · 2019-04-16 08:39

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

Seem to be working.

查看更多
做个烂人
3楼-- · 2019-04-16 08:53

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];
查看更多
成全新的幸福
4楼-- · 2019-04-16 08:53

You can use navigation controller:

    let vc = self.storyboard!.instantiateViewController(withIdentifier: "FirstPageViewController") as! FirstPageViewController
    self.navigationController?.pushViewController(vc, animated: true)
查看更多
登录 后发表回答