The life cycle of the IOS app

Source: Internet
Author: User
Tags terminates

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. All you need to do is overwrite the object in the appropriate way to handle this ?? Some 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 not running: The program did not start.

inactive Inactive: The program is running 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 events. This is also a normal mode of the foreground.

Backgroud Backstage: The program is in the background and can execute code, most programs enter this state will stay in this state for a while. 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: Agent callback for individual program running state

① tells the agent that the process started but has not yet entered a state to save

1 -(BOOL) Application: (UIApplication *) application willfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions2{3     NSLog (@ "① tells the agent process to start but has not yet entered state save " ); 4     return YES; 5 }

② tells the agent to start the basic Completion program ready to start running

1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions2 {3Self.window =[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds];4     5NSLog (@"② tells the agent to start the basic Completion program ready to start running");6     7     //Override point for customization after application launch.8     9Self.window.backgroundColor =[Uicolor Whitecolor];Ten [Self.window makekeyandvisible]; One     returnYES; A}

③ when an application is going into an inactive state, during which time the application does not receive messages or events, such as calling

1- (void) Applicationwillresignactive: (UIApplication *) Application2 {3     //Sent when the application are about to 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.4     //Use the 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.5NSLog (@"③ When an application is going into an inactive state, during which time the application does not receive messages or events, such as calling");6}

④ when an application enters active state execution

1 -(void) Applicationdidbecomeactive: (uiapplication *) application2{ 3     // 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. 4     NSLog (@ "④ when the application goes into active execution "); 5 }

⑤ 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

1- (void) Applicationdidenterbackground: (UIApplication *) Application2 {3     //Use the method to release the shared resources, save user data, invalidate timers, and store enough application state in Formation to the restore your application to the it is terminated later.4     //If Your application supports background execution, this method is called instead of Applicationwillterminate:when th E user quits.5NSLog (@"⑤ Called when the program is pushed to the background");6     7[Application beginbackgroundtaskwithexpirationhandler:^{8         9NSLog (@"begin Background Task with expiration Handler");Ten          One     }]; A}

⑥ is called when the program is going back to the foreground from the background

1 -(void) Applicationwillenterforeground: (uiapplication *) application2{  3     // called as part of the transition from the background to the inactive state; here yo U can undo many of the changes made on entering the background. 4     NSLog (@ "⑥ when the program from the background will be back to the foreground when called "); 5 }

⑦ 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

1 -(void) Applicationwillterminate: (uiapplication *) application2{ 3     // Called when the application are about to terminate. Save data if appropriate. See also Applicationdidenterbackground:. 4     NSLog (@ "⑦ when the program is going to exit is called "); 5 }

⑧ execution when the program is loaded

1 -(void) applicationdidfinishlaunching: (uiapplication *) application2{  3     NSLog (@ "⑧ when program is loaded "); 4 }

When the program starts:

2014-07-01 15:55:14.706 lifecycle[5845:60b] ① tells the agent that the process started but has not yet entered the state to save

2014-07-01 15:55:14.708 lifecycle[5845:60b] ② tell the agent to start the basic Completion program ready to start running

2014-07-01 15:55:14.709 lifecycle[5845:60b] ④ When an application enters active state execution

Press the Home button to return to the main interface:

2014-07-01 15:56:11.756 lifecycle[5845:60b] ③ When an application is going into an inactive state

2014-07-01 15:56:11.814 lifecycle[5845:60b] ⑤ called when the program is pushed to the background

Open the program again:

2014-07-01 15:57:19.200 lifecycle[5845:60b] ⑥ called when the program is going back to the foreground from the background

2014-07-01 15:57:19.201 lifecycle[5845:60b] ④ When an application enters active state execution

3: Loading the app into the foreground

4: Loading the application into the background

5: Warning-based response interrupt

When 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 requests

③ Pause Video Playback

④ if it's a game, just pause it.

⑤ reduce the frame rate of OpenGL ES

⑥ suspend any distributed queue and non-critical operations queue (you can continue to handle network requests or other time-sensitive background tasks)

When the program returns to the active state, we need to restart the task in the-(void) Applicationdidbecomeactive: (UIApplication *) Application method. However, the game will go back to a paused state and not start automatically.

6: Run into the background

What 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

1 [application beginbackgroundtaskwithexpirationhandler:^{2         3         NSLog ( @" begin Background Task with expiration Handler " ); 4         5     }];

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 remove the strong references as soon as possible:

① Picture Object

② you can reload a large video or data file

③ any useless and easy-to-create object

In the background, in order to reduce the memory used by the program, the system will automatically recycle some systems to help you open up the memory. Like what:

The ① system recycles the backup storage of the core animation.

② Remove any system-referenced cached pictures

③ Remove System Management data cache strong references

7: Back to foreground run

When 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: Program Termination

The program terminates if it enters the background or hangs as long as one of the following conditions is met:

①ios4.0 the previous system

②app was developed based on the iOS4.0 system.

③ devices do not support multi-tasking

④ in the Info.plist file, 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 loop

Main 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.

-------------------------------------

This article is based on the official document collation, translation basically rely on Google.

Original address:

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ Managingyourapplicationsflow/managingyourapplicationsflow.html#//apple_ref/doc/uid/tp40007072-ch4-sw20

-------------------------------------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.