For iOS apps, it is crucial to know whether your app is running in the foreground or the background. Because system resources are more limited on iOS devices, an app must behave differently in the background than in the foreground.(应用在后台一定要和前台不同才行) The operating system also limits what your app can do in the background in order to improve battery life and to improve the user’s experience with the foreground app. (操作系统限制后台应用能做的事情以便节省电力,也保证前台应用的用户体验)The operating system notifies your app whenever it moves between the foreground and background. (app前后台切换的时候,操作系统给予notification)These notifications are your chance to modify your app’s behavior.
While your app is active and in the foreground, the system sends touch events to it for processing. The UIKit infrastructure does most of the hard work of delivering events to your custom objects. All you have to do is override methods in the appropriate objects to process those events. For controls, UIKit simplifies things even further by handling the touch events for you and calling your custom code only when something interesting happens, such as when the value of a text field changes.(系统替你做了大多数的事情)
As you implement your app, follow these guidelines:
-
(Required) Respond appropriately to the state transitions that occur. Not handling these transitions properly can lead to data loss and a bad user experience. For a summary of how to respond to state transitions, see“Managing App State Changes.” (必须:app状态转换,要正确响应。)
-
(Required) When moving to the background, make sure your app adjusts its behavior appropriately. For guidelines about what to do when your app moves to the background, see “Being a Responsible Background App.”(必须:当应用切换到后台的过程中,一定要确保应用正确调整行为。)
-
(Recommended) Register for any notifications that report system changes your app needs. When an app is suspended, the system queues key notifications and delivers them when the app resumes execution. Apps should use these notifications to make a smooth transition back to execution. For more information, see“Processing Queued Notifications at Wakeup Time.”(建议:注册所有app需要的报告系统变化的通知)
-
(Optional) If your app needs to do actual work while in the background, ask the system for the appropriate permissions to continue running. For more information about the types of background work you can do and how to request permission to do that work, see “Background Execution and Multitasking.”(可选:如果app在后台确实需要工作,向系统请求合适的许可以便继续运行)。
APP的几种状态
State |
Description |
---|---|
Not running |
The app has not been launched or was running but was terminated by the system. |
Inactive |
The app is running in the foreground but is currently not receiving events. (It may be executing other code though.) An app usually stays in this state only briefly as it transitions to a different state. (前台,不接收消息,短暂处于这一状态(从一种状态切换到另一种状态时))----应用刚启动or 应用程序由运行切到后台时 |
Active |
The app is running in the foreground and is receiving events. This is the normal mode for foreground apps. (前台,normal 模式) |
Background |
The app is in the background and executing code. Most apps enter this state briefly on their way to being suspended. However, an app that requests extra execution time may remain in this state for a period of time. In addition, an app being launched directly into the background enters this state instead of the inactive state. For information about how to execute code while in the background, see “Background Execution and Multitasking.” (大多数程序短暂处于这一状态(切换到suspend状态过程中的一种状态)),有些应用需要额外执行时间来保持在这种状态下。(在后台且执行代码)---应用要进前台or应用要被挂起 |
Suspended |
The app is in the background but is not executing code. The system moves apps to this state automatically and does not notify them before doing so. While suspended, an app remains in memory but does not execute any code. When a low-memory condition occurs, the system may purge suspended apps without notice to make more space for the foreground app. (应用在后台,不执行代码),系统自动将应用切换到这个状态而不给应用通知。当应用挂起,应用仍然在内存里,但是不执行任何代码。 当低内存情况出现,系统可能不通知就把应用给清理了以便给前台更多的内存空间。 |
State changes in an iOS app
Note: Apps running in iOS 3.2 and earlier do not enter the background or suspended states. In addition, some devices do not support multitasking or background execution at all, even when running iOS 4 or later. Apps running on those devices also do not enter the background or suspended states. Instead, apps are terminated upon leaving the foreground.(不支持多任务的不进入后台或者挂起状态,而是直接杀死)
Most state transitions are accompanied by a corresponding call to the methods of your app delegate object. These methods are your chance to respond to state changes in an appropriate way. These methods are listed below, along with a summary of how you might use them.(代理的方法给了响应app状态改变的机会)
-
application:willFinishLaunchingWithOptions:
—This method is your app’s first chance to execute code at launch time. -
application:didFinishLaunchingWithOptions:
—This method allows you to perform any final initialization before your app is displayed to the user. -
applicationDidBecomeActive:
—Lets your app know that it is about to become the foreground app. Use this method for any last minute preparation. -
applicationWillResignActive:
—Lets you know that your app is transitioning away from being the foreground app. Use this method to put your app into a quiescent state. (将由active转到inactive) -
applicationDidEnterBackground:
—Lets you know that your app is now running in the background and may be suspended at any time. -
applicationWillEnterForeground:
—Lets you know that your app is moving out of the background and back into the foreground, but that it is not yet active. -
applicationWillTerminate:
—Lets you know that your app is being terminated. This method is not called if your app is suspended.