ios applicationWillEnterForeground 与 applicationDidBecomeActive、applicationWillResignActive 与 applicationDidEnterBackground

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3712979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:43:35  来源:igfitidea点击:

applicationWillEnterForeground vs. applicationDidBecomeActive, applicationWillResignActive vs. applicationDidEnterBackground

iosobjective-ciphoneuiapplicationdelegate

提问by Paul

Which is the proper delegate to implement when an application is waking up from being in the background and you want it to prep it to be active?

当应用程序从后台唤醒并且您希望它准备好使其处于活动状态时,哪个是合适的委托?

applicationWillEnterForeground vs applicationDidBecomeActive -- What's the difference?

applicationWillEnterForeground 与 applicationDidBecomeActive —— 有什么区别?

Which is the proper delegate to implement for when an application is going to sleep and you want to prep it to cleanup and save data?

当应用程序将要休眠并且您希望它准备好清理和保存数据时,要实现哪个委托是正确的?

applicationWillResignActive vs. applicationDidEnterBackground -- What's the difference?

applicationWillResignActive 与 applicationDidEnterBackground -- 有什么区别?

Also, I've noticed that applicationWillResignActive gets called when an incoming SMS or call comes in but the user chooses to click Ok and continue. I don't want my app to take any action in these cases. I just want it to keep running without any intermediate cleanup since the user didn't exit the app. So, I would think it makes more sense to do cleanup work just in applicationDidEnterBackground.

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

I would appreciate your input on best practices to follow on choosing which delegates to implement for waking up and going to sleep as well as considering events like being interrupted by SMS/calls.

我非常感谢您对最佳实践的意见,以选择要实施哪些代表以实现醒来和入睡,以及考虑诸如被短信/电话打断之类的事件。

Thanks

谢谢

回答by Dan Sandland

When waking up i.e. relaunching an app (either through springboard, app switching or URL) applicationWillEnterForeground:is called. It is only executed once when the app becomes ready for use, after being put into the background, while applicationDidBecomeActive:may be called multiple times after launch. This makes applicationWillEnterForeground:ideal for setup that needs to occur just once after relaunch.

唤醒时,即重新启动应用程序(通过跳板、应用程序切换或 URL)applicationWillEnterForeground:被调用。它只在应用程序准备好使用时执行一次,在进入后台后,而applicationDidBecomeActive:在启动后可能会被多次调用。这applicationWillEnterForeground:非常适合需要在重新启动后进行一次的设置。

applicationWillEnterForeground:is called:

applicationWillEnterForeground:叫做:

  • when app is relaunched
  • before applicationDidBecomeActive:
  • 重新启动应用程序时
  • applicationDidBecomeActive:

applicationDidBecomeActive:is called:

applicationDidBecomeActive:叫做:

  • when app is first launched after application:didFinishLaunchingWithOptions:
  • after applicationWillEnterForeground:if there's no URL to handle.
  • after application:handleOpenURL:is called.
  • after applicationWillResignActive:if user ignores interruption like a phone call or SMS.
  • 当应用程序首次启动后 application:didFinishLaunchingWithOptions:
  • 之后applicationWillEnterForeground:如果没有要处理的 URL。
  • 之后application:handleOpenURL:被调用。
  • 之后applicationWillResignActive:,如果用户忽略中断就像一个电话或短信。

applicationWillResignActive:is called:

applicationWillResignActive:叫做:

  • when there is an interruption like a phone call.
    • if user takes call applicationDidEnterBackground:is called.
    • if user ignores call applicationDidBecomeActive:is called.
  • when the home button is pressed or user switches apps.
  • docs say you should
    • pause ongoing tasks
    • disable timers
    • pause a game
    • reduce OpenGL frame rates
  • 当有电话中断时。
    • 如果用户接听电话applicationDidEnterBackground:被调用。
    • 如果用户忽略呼叫applicationDidBecomeActive:被调用。
  • 当按下主页按钮或用户切换应用程序时。
  • 文档说你应该
    • 暂停正在进行的任务
    • 禁用计时器
    • 暂停游戏
    • 降低 OpenGL 帧率

applicationDidEnterBackground:is called:

applicationDidEnterBackground:叫做:

  • after applicationWillResignActive:
  • docs say you should:
    • release shared resources
    • save user data
    • invalidate timers
    • save app state so you can restore it if app is terminated.
    • disable UI updates
  • you have 5 seconds to do what you need to and return the method
    • if you don't return within ~5 seconds the app is terminated.
    • you can ask for more time with beginBackgroundTaskWithExpirationHandler:
  • applicationWillResignActive:
  • 文档说你应该:
    • 释放共享资源
    • 保存用户数据
    • 无效计时器
    • 保存应用程序状态,以便在应用程序终止时可以恢复它。
    • 禁用 UI 更新
  • 你有 5 秒的时间做你需要做的事情并返回方法
    • 如果您没有在 ~5 秒内返回,则应用程序将终止。
    • 你可以要求更多的时间 beginBackgroundTaskWithExpirationHandler:

The official documentation.

官方文档。

回答by tomjpsun

Managing Your App's Life Cycleis helpful to your questions. For quick concept, you can see Figures in that document. You can also read the comment from the code generated by the XCode Wizard. Listed as follows:

管理您的应用程序的生命周期有助于解决您的问题。为了快速概念,您可以在该文档中查看图表。您还可以从 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:.
     */
}

For more detailed explanations, please refer to official document for UIApplicationDelegate

更详细的解释请参考UIApplicationDelegate官方文档

回答by Rahmi Aksu

I was still a bit confused with Dano's answer so I did a little test to get the flow of events in certain scenarios for my reference, but it might be useful to you too. This is for apps that DO NOT use UIApplicationExitsOnSuspendin their info.plist. This was conducted on an iOS 8 simulator + confirmed with iOS 7 device. Please excuse Xamarin's event handler names. They are very similar.

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

  • Initial and all subsequent launches from a not-running state:
  • 从非运行状态初始启动和所有后续启动:

FinishedLaunching

OnActivated

完成发射

已激活

  • Interruption (phone call, top slide-down, bottom slide-up):
  • Home button double-press listing inactive apps, then reselecting our app:
  • 中断(电话、上滑、下滑):
  • 双击主页按钮列出不活动的应用程序,然后重新选择我们的应用程序:

OnResignActivation


OnActivated

OnResignActivation


已激活

  • Home button double-press listing inactive apps, selecting another app, then relaunching our app:
  • Home button single press, then relaunch:
  • Lock (on/off button), then unlock:
  • 双击主页按钮列出不活动的应用程序,选择另一个应用程序,然后重新启动我们的应用程序:
  • 单击主页按钮,然后重新启动:
  • 锁定(开/关按钮),然后解锁:

OnResignActivation

DidEnterBackground


WillEnterForeground

OnActivated

OnResignActivation

输入背景


将进入前景

已激活

  • Home button double-press, and terminate our app: (subsequent relaunch is first case)
  • 双击主页按钮,并终止我们的应用程序:(随后重新启动是第一种情况)

OnResignActivation

DidEnterBackground

DidEnterBackground (iOS 7 only?)

OnResignActivation

输入背景

DidEnterBackground(仅限 iOS 7?)

Yes, DidEnterBackgroundis called twice on iOS7 device. Both times UIApplication state is Background. However, iOS 8 simulator does not. This needs testing on iOS 8 device. I will update my answer when I get my hand on it, or someone else could confirm.

是的,DidEnterBackground在 iOS7 设备上被调用两次。两次 UIApplication 状态都是背景。但是,iOS 8 模拟器没有。这需要在 iOS 8 设备上进行测试。当我得到答案时,我会更新我的答案,或者其他人可以确认。

回答by Kareem Waheed

applicationWillEnterForegroundis called:

applicationWillEnterForeground叫做:

when app is relaunched(comes from background to foreground) This method is not invoked when app starts for the first time i.e when applicationDidFinishLaunchis called but only when comes from background applicationDidBecomeActive

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

applicationDidBecomeActiveis called

applicationDidBecomeActive叫做

when app is first launched after didFinishLaunchingafter applicationWillEnterForegroundif there's no URL to handle. after application:handleOpenURL:is called. after applicationWillResignActiveif user ignores interruption like a phone call or SMS. after disappearing of alertView anywhere from the application

如果没有要处理的 URL,则在didFinishLaunching之后首次启动应用程序时applicationWillEnterForeground。之后application:handleOpenURL:被调用。之后applicationWillResignActive,如果用户忽略中断就像一个电话或短信。从应用程序的任何地方消失 alertView 后

回答by Anson Yao

applicationWillResignActive is called when system is asking for permissions. (in iOS 10). Just in case someone hit into the same trouble as me...

applicationWillResignActive 在系统请求权限时被调用。(在 iOS 10 中)。以防万一有人遇到和我一样的麻烦...

回答by Qiulang

In iOS 8+ there is a subtle but important difference for taking phone call.

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

In iOS 7 if user takes phone call both applicationWillResignActive: and applicationDidEnterBackground: are called. But in iOS 8+ only applicationWillResignActive: is called.

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

回答by user2994130

For iOS 13+ the following methods will be executed:

对于 iOS 13+,将执行以下方法:

- (void)sceneWillEnterForeground:(UIScene *)scene
- (void)sceneDidBecomeActive:(UIScene *)scene