applicationWillEnterForeground与applicationDidBecomeActive,applicationWillResignActive与applicationDidEnterBackground

当一个应用程序从后台醒来,并且希望它准备好处于活动状态时,哪个代码可以实现?

applicationWillEnterForeground vs applicationDidBecomeActive – 有什么区别?

当一个应用程序要进入睡眠状态时,哪个代码需要执行,并且要准备清理并保存数据?

applicationWillResignActive与applicationDidEnterBackground – 有什么区别?

另外,我注意到applicationWillResignActive在传入短信或来电时被调用,但用户select单击“确定”并继续。 我不希望我的应用程序在这些情况下采取任何行动。 我只是希望它保持运行没有任何中间清理,因为用户没有退出应用程序。 所以,我认为在applicationDidEnterBackground中进行清理工作更有意义。

感谢您对最佳实践的意见,以便select哪些代表实施起床和睡眠,以及考虑被短信/电话中断等事件。

谢谢

当醒来,即重新启动应用程序(通过跳板,应用程序切换或URL) applicationWillEnterForeground:被调用。 它只在应用程序准备就绪后才会执行一次,放入后台后,而applicationDidBecomeActive:可能会在启动后多次调用。 这使得applicationWillEnterForeground:理想的设置,需要在重新启动后只发生一次。

applicationWillEnterForeground:被调用:

  • 当应用程序重新启动
  • applicationDidBecomeActive:之前applicationDidBecomeActive:

applicationDidBecomeActive:被调用:

  • 当应用程序第一次启动后application:didFinishLaunchingWithOptions:
  • applicationWillEnterForeground:之后applicationWillEnterForeground:如果没有URL处理。
  • application:handleOpenURL:之后application:handleOpenURL:被调用。
  • applicationWillResignActive:applicationWillResignActive:如果用户忽略电话或短信的中断。

applicationWillResignActive:被调用:

  • 当有电话中断时。
    • 如果用户接到调用applicationDidEnterBackground:被调用。
    • 如果用户忽略调用applicationDidBecomeActive:被调用。
  • 当主页button被按下或用户切换应用程序。
  • 文档说你应该
    • 暂停正在进行的任务
    • 禁用定时器
    • 暂停游戏
    • 降低OpenGL帧速率

applicationDidEnterBackground:被调用:

  • applicationWillResignActive:之后applicationWillResignActive:
  • 文档说你应该:
    • 释放共享资源
    • 保存用户数据
    • 无效的定时器
    • 保存应用状态,以便在应用终止时恢复。
    • 禁用UI更新
  • 你有5秒钟做你所需要的,并返回该方法
    • 如果你不在〜5秒内返回应用程序被终止。
    • 你可以要求更多的时间beginBackgroundTaskWithExpirationHandler:

官方文件。

这个Apple文档对你的问题很有帮助。 对于快速概念,您可以在该文档中看到图3-1。 您还可以从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 inactive 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:. */ } 

在上面的代码中,只有应用程序启动 ,你有机会说是或否,其他人只是通知。 换句话说,您无法通过上面的代码列表强制用户忽略来电或短信。 不知道是否有其他的解决方法。

我对Dano的回答仍然有些困惑,所以我做了一些testing,以便在某些场景中获得事件的stream程供我参考,但是对您也可能有用。 这适用于不在其info.plist中使用UIApplicationExitsOnSuspend应用程序。 这是在iOS 8模拟器上进行的+使用iOS 7设备确认的。 请原谅Xamarin的事件处理程序名称。 他们非常相似。

  • 初始和所有后续从非运行状态启动:

FinishedLaunching

OnActivated

  • 中断(电话,顶部滑动,底部滑动):
  • 主页button双击列出不活动的应用程序,然后重新select我们的应用程序:

OnResignActivation


OnActivated

  • 主页button双击列出不活动的应用程序,select其他应用程序,然后重新启动我们的应用程序:
  • 主页button单击,然后重新启动:
  • locking(开/关button),然后解锁:

OnResignActivation

DidEnterBackground


WillEnterForeground

OnActivated

  • 主页button双击,并终止我们的应用程序:(随后重新启动是第一种情况)

OnResignActivation

DidEnterBackground

DidEnterBackground(仅适用于iOS 7?)

是的, DidEnterBackground在iOS7设备上被调用两次。 两次UIApplication状态都是Background。 但是,iOS 8模拟器没有。 这需要在iOS 8设备上进行testing。 当我拿到手时,我会更新我的答案,或者其他人可以证实。

applicationWillEnterForeground被调用:

当应用程序重新启动(从后台到前台)当应用程序第一次启动时不会调用此方法,即当applicationDidFinishLaunch被调用,但只有当来自后台applicationDidBecomeActive

applicationDidBecomeActive被调用

当应用程序在didFinishLaunching之后首先启动后,在didFinishLaunching之后,如果没有URL处理。 application:handleOpenURL:之后application:handleOpenURL:被调用。 如果用户忽略像电话或短信那样的中断,则在applicationWillResignActive之后进行重新分配活动。 从应用程序的任何地方消失alertView后

在iOS 8+中,接听电话有一个微妙而重要的区别。

在iOS 7中,如果用户接听电话,则会调用applicationWillResignActive:和applicationDidEnterBackground:。 但是在iOS 8+中只有applicationWillResignActive:被调用。

当系统请求权限时,applicationWillResignActive被调用。 (在iOS 10中)。 以防万一遇到同样的麻烦