在开始状态下从后台重新启动应用程序

时间:2016-03-07 14:28:59

标签: ios swift

我正在努力实现这个功能。当用户按下主页按钮时,应用程序将转到后台(它仍然会运行),当他再次打开它时,应用程序将不会在最后一个视图中启动,而是从一开始就开始。

如何检测应用程序转到后台并重置它以启动视图。我认为它应该是app delegate中的东西 感谢您的所有评论,但那只是答案的1/2。如何从app delegate poptorootvc?

2 个答案:

答案 0 :(得分:2)

我认为这就是你正在寻找的东西

func applicationDidBecomeActive(application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

将它放在你的AppDelegate中,然后切换到你想要的任何视图。

或者,您可以使用其中一个检测应用状态更改的其他AppDelegate函数

 func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
}

 func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

答案 1 :(得分:2)

您可以将代码放在

func applicationWillEnterForeground(application: UIApplication) {

}