For iOS apps, it's important to know if your application is running in the foreground or in the background. Because system resources are limited on iOS devices, an application must behave differently in the background than in the foreground. The operating system also restricts your application from running in the background to improve battery life and improve the user experience with the foreground application. When the application switches between the foreground and the background, your application will be notified by the operating system. You can use these notifications to modify the behavior of your application. When your application is active in the foreground, the system sends a touch event to process it. The Uikit infrastructure has done most of the event to pass on to your custom object work. So what you need to do is override the methods in the corresponding object to handle these events. For controls, Uikit will invoke your custom code by handling your touch events, or other interesting things, such as when a value in a text field changes.
1. Status of the application
not running is not running: The program did not start.
Inactive Not activated: The program runs in the foreground, but no events are received. In the absence of event handling, the program usually stays in this state.
Active activation: The program runs in the foreground and receives the event. This is also a normal mode of the foreground.
Backgroud Backstage: The program is in the background and can execute code, and most programs will stay in this state for a while after entering this state. The time will then go into the suspended state (Suspended). Some programs can be in Backgroud state for a long time after special requests.
Suspended hangs: The program cannot execute code in the background. The system automatically turns the program into this state and does not give notice. When suspended, the program is still in memory, when the system memory is low, the system will remove the suspended program, to provide more memory for the foreground program.
2. The callback of the agent when each program is running state① tells the agent that the process started but has not yet entered a state to save
- -(BOOL) Application: (UIApplication *) application willfinishlaunchingwithoptions: (nsdictionary *) launchOptions
- {
- NSLog (@"① tells the agent process to start but has not yet entered the state save");
- return YES;
- }
② tells the agent to start the basic Completion program ready to start running
- -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
- {
- Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
- NSLog (@"② tells the agent to start the basic Completion program ready to start Running");
- //Override point for customization after application launch.
- Self.window.backgroundColor = [Uicolor Whitecolor];
- [Self.window makekeyandvisible];
- return YES;
- }
③ when an application is going into an inactive state, during which time the application does not receive messages or events, such as calling
- -(void) applicationwillresignactive: (uiapplication *) Application
- {
- //Sent when the application is on-move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US Er quits the application and it begins the transition to the background state.
- //Use this method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
- NSLog (@"③ when an application is going to be inactive, during which time the application does not receive messages or events, such as calling");
- }
④ when an application enters active state execution
- -(void) applicationdidbecomeactive: (uiapplication *) Application
- {
- //Restart Any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface.
- NSLog (@"④ when the application goes into active execution");
- }
⑤ is called when the program is pushed to the background. So to set the background to continue running, you can set it in this function
- -(void) Applicationdidenterbackground: (uiapplication *) Application
- {
- //Use this method to release shared resources, save user data, invalidate timers, and store enough application state Information to the restore your application to the it is terminated later.
- //If Your application supports background execution, this method is called instead of Applicationwillterminate:when The user quits.
- NSLog (@"⑤ called when the program is pushed to the background);
- [Application beginbackgroundtaskwithexpirationhandler:^{
- NSLog (@"begin Background Task with Expiration Handler");
- }];
- }
⑥ is called when the program is going back to the foreground from the background
- -(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 M Ade on entering the background.
- NSLog (@"⑥ when the program from the background will be back to the foreground when called");
- }
⑦ when a program is about to exit is called, it is usually used to save data and some cleanup work before exiting. This needs to set the key value of Uiapplicationexitsonsuspend
- -(void) Applicationwillterminate: (uiapplication *) Application
- {
- //Called when the application are about to terminate. Save data if appropriate. See also Applicationdidenterbackground:.
- NSLog (@"⑦ when the program is going to exit is called");
- }
⑧ execution when the program is loaded
- -(void) applicationdidfinishlaunching: (uiapplication *) Application
- {
- NSLog (@"⑧ when the program is Loaded");
- }
When the program starts: 2014-07-01 15:55:14.706 Lifecycle[5845:60b]① tells the agent that the process started but has not entered the state save 2014-07-01 15:55:14.708 lifecycle[5845:60b] ② tells the agent to start the basic Completion program ready to start running 2014-07-01 15:55:14.709 Lifecycle[5845:60b]④ when the application enters active state execution press home key to return to the main interface: 2014-07-01 15:56:11.756 Lifecycle[5845:60b]③ when the application is going into an inactive state 2014-07-01 15:56:11.814 Lifecycle[5845:60b]⑤ called to open the program again when the program is pushed to the background: 2014-07-01 15:57:19.200 Lifecycle[5845:60b]⑥ When the program is going back to the foreground from the background, call 2014-07-01 15:57:19.201 Lifecycle[5845:60b]④ when the application enters the active state execution
3. Loading the application into the foreground
4. Loading the application into the background
5. Warning-based response interruptWhen this interruption occurs, we need to do the following in the-(void) Applicationwillresignactive: (UIApplication *) Application method: ① Stop the timer and other recurring tasks ② stop any running request ③ pause the playback of the video ④ if it's a game, pause it ⑤ reduce the frame rate of OpenGL ES ⑥ suspend any distributed queue and unimportant operations queue (you can continue to process network requests or other time-sensitive background tasks) when the program returns to the active state, we need to-( void) Applicationdidbecomeactive: (uiapplication *) Restart the above task in the application method. However, the game will go back to a paused state and not start automatically.
6, into the background to runWhat should we do when the app goes backstage? Save user data or status information, all files or information not written to the disk, in the background, the last to write to the disk, because the program may be killed in the background. Releases the memory as free as possible. -(void) Applicationdidenterbackground: (UIApplication *) The application method has about 5 seconds to complete these tasks. If there is an unfinished task over time, your program will be terminated and purged from memory. If you still need to run the task for a long time, you can call it in the method
- [Application beginbackgroundtaskwithexpirationhandler:^{
- NSLog (@"begin Background Task with Expiration Handler");
- }];
Memory usage when the application is in the background: requests a background run time and a startup thread to run long-running tasks. In the background, each application should release the maximum memory. The system strives to keep more applications running at the same time in the background. However, when memory is low, some suspended programs are terminated to reclaim memory, and the most memory programs are terminated first. In fact, if the object that the application should be no longer used, it should remove the strong reference as soon as possible, so that the compiler can reclaim the memory. If you want to cache the performance of some object-boosting programs, you can remove these objects from strong references when you enter the background. The following objects should be removed as soon as possible: ① Picture Object ② You can reload the large video or data files ③ any useless and can easily create objects in the background, in order to reduce the memory that the program consumes, the system will automatically recycle some systems to help you open up the memory. For example: The ① system recycles the backup storage of the core animation. ② Remove any system-referenced cached pictures ③ Remove the system Management data cache strong references
7, return to the front desk operationWhen an application in the paused state must be ready to process any queued notification, it returns to the foreground or background execution state. Suspended applications do not execute any code and therefore cannot handle changes in direction, changes in time, preferences, and many other notifications that affect the appearance or state of the application. To ensure that these changes are not lost, the system queues many related notifications and passes them to the application as soon as it starts executing the code again (either in the foreground or background). To prevent the application from becoming overloaded with it when it recovers, the system condenses events and provides a single notification (each related type) that reflects the net change as your application is paused.
8. Termination of programThe program terminates as long as it is in the background or pending state as long as one of the following conditions is met: ①ios4.0 previous system ②app was developed based on iOS4.0. ③ devices do not support multitasking ④ in info.plist files, the program contains the Uiapplicationexitsonsuspend key. If the app terminates, the system calls the app's proxy method-(void) Applicationwillterminate: (uiapplication *) application, which allows you to do some cleanup work. You can save some data or app status. This method also has a 5-second limit. After a timeout, the method returns the program from memory cleanup. Note: Users can close the application manually.
9. The main run loopMain Run Loop is responsible for handling user-related events. The UIApplication object launches the main run Loop at program startup, which handles events and updates the view's interface. See main run loop to know that it is running on the main thread of the program. This ensures that events that receive user-related actions are processed sequentially. User-operated devices, related operation events are generated by the system and distributed through the specified port of Uikit. Events are queued internally and distributed to main run loop for processing. The UIApplication object is the first object to receive a time, and it determines how the event is handled. Touch events are distributed to the main window, and the window is then distributed to the view that corresponds to the start touch event. Other events are distributed to other object variables for processing by other means. Most of the events can be distributed in your app, similar to touch events, remote manipulation events (line headphones, etc.) are handled by the app's responder objects object. Responder objects is everywhere in your app, such as: UIApplication object, view object, view Controller object, all Resopnder objects. The target of most events specifies Resopnder object, but events can also be passed to other objects. For example, if the view object does not handle events, it can be passed to the parent view or view controller.
Ios-app life cycle