当应用程序从后台唤醒并且您希望它准备好处于活动状态时,哪个是正确的委托?
applicationWillEnterForeground vs applicationDidBecomeActive - 有什么区别?
当应用程序进入休眠状态并且您想要准备它以清理和保存数据时,哪个是适当的委托?
applicationWillResignActive与applicationDidEnterBackground - 有什么区别?
此外,我注意到当传入的短信或呼叫进入但用户选择单击“确定”并继续时,将调用applicationWillResignActive。我不希望我的应用程序在这些情况下采取任何行动。我只是希望它继续运行而不进行任何中间清理,因为用户没有退出应用程序。所以,我认为在applicationDidEnterBackground中进行清理工作更有意义。
我很感激您对最佳实践的意见,以便选择要唤醒和入睡的代表,以及考虑被短信/电话中断等事件。
由于
答案 0 :(得分:413)
唤醒时,即重新启动应用(通过跳板,应用切换或网址),会调用 applicationWillEnterForeground:
。它只在应用程序准备好使用后执行一次,在放入后台后,而 applicationDidBecomeActive:
可能会在启动后多次调用。这使得 applicationWillEnterForeground:
非常适合在重新启动后只需要发生一次的设置。
applicationWillEnterForeground:
被称为:
applicationDidBecomeActive:
之前 applicationDidBecomeActive:
被称为:
application:didFinishLaunchingWithOptions:
applicationWillEnterForeground:
后,如果没有要处理的网址。application:handleOpenURL:
之后。applicationWillResignActive:
之后 applicationWillResignActive:
被称为:
applicationDidEnterBackground:
,则会被调用。applicationDidBecomeActive:
。 applicationDidEnterBackground:
被称为:
applicationWillResignActive:
beginBackgroundTaskWithExpirationHandler:
答案 1 :(得分:25)
This Apple Document对您的问题很有帮助。对于快速概念,您可以在该文档中看到图2-3。 您还可以从XCode向导生成的代码中读取注释。列举如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
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.
Use this method to pause ongoing tasks, disable timers, and throttle down
OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
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.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the active state;
here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
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.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}
在上面的代码中,只有应用程序启动才有机会说“是”或“否”,其他只是通知。换句话说,您无法通过上面的代码列表强制用户忽略来电或短信。不知道是否还有其他解决方法。
答案 2 :(得分:13)
我仍然对Dano的答案感到困惑,所以我做了一些测试以获取某些场景中的事件流程供我参考,但它也可能对你有用。这适用于不在其info.plist中使用UIApplicationExitsOnSuspend
的应用。这是在iOS 8模拟器上进行的+使用iOS 7设备确认的。请原谅Xamarin的事件处理程序名称。它们非常相似。
FinishedLaunching
OnActivated
OnResignActivation
OnActivated
OnResignActivation
DidEnterBackground
WillEnterForeground
OnActivated
OnResignActivation
DidEnterBackground
DidEnterBackground(仅限iOS 7?)
是的,在iOS7设备上调用DidEnterBackground
两次。两次UIApplication状态都是背景。但是,iOS 8模拟器没有。这需要在iOS 8设备上进行测试。当我接触到它或其他人可以确认时,我会更新我的答案。
答案 3 :(得分:9)
applicationWillEnterForeground
被称为:
当app重新启动时(来自后台到前台)
当应用程序第一次启动时,即调用applicationDidFinishLaunch
但仅来自后台时,不会调用此方法
applicationDidBecomeActive
applicationDidBecomeActive
被称为
在didFinishLaunching
之后首次启动应用时
在applicationWillEnterForeground
之后,如果没有要处理的URL。
调用application:handleOpenURL:
后。
在applicationWillResignActive
之后,如果用户忽略了电话或短信等中断。
从应用程序的任何地方消失alertView后
答案 4 :(得分:7)
在系统要求权限时调用applicationWillResignActive。 (在iOS 10中)。万一有人遇到和我一样的麻烦...
答案 5 :(得分:5)
在iOS 8+中,接听电话有一个微妙但重要的区别。
在iOS 7中,如果用户接听电话,则会调用applicationWillResignActive:和applicationDidEnterBackground :.但是在iOS 8+中只有applicationWillResignActive:被调用。
答案 6 :(得分:2)
对于iOS 13+,将执行以下方法:
- (void)sceneWillEnterForeground:(UIScene *)scene
- (void)sceneDidBecomeActive:(UIScene *)scene