How to restart an app when resumes from multi-tasking

笑着哭i 提交于 2019-12-06 12:29:02

问题


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)


来源:https://stackoverflow.com/questions/30647036/how-to-restart-an-app-when-resumes-from-multi-tasking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!