检测用户是否从主屏幕退出应用程序

detect If user exits application from homescreen

我是 iOS 开发的新手

如果用户从 homescreen

退出应用程序,我想执行一些任务

有什么方法可以确定吗?

请帮忙,提前致谢!!

这个函数:

func applicationWillTerminate(application: UIApplication) {}

在您的应用程序即将终止时调用。

来自 Apple 文档:

This method lets your app know that it is about to be terminated and purged from memory entirely. You should use this method to perform any final clean-up tasks for your app, such as freeing shared resources, saving user data, and invalidating timers. Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.

有关更多信息,请查看:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html

当您输入 didEnterBackground 时,您需要检查导航堆栈中最后一个可见的控制器。如果是主屏幕,就在那里做你想做的。

- (void)applicationDidEnterBackground:(UIApplication *)application {    
    id controller = self.navigationController.visibleViewController;
    if ([controller isKindOfClass:[HomeViewController class]])
    {
       //do your stuff here
    }
}