The life cycle of an iOS app

Source: Internet
Author: User
Tags uikit

iOS applications are typically made up of code and system frameworks that are written by themselves (System frameworks), and the framework provides some basic infrastructure for all apps to run, And you provide your own code to customize the look and behavior of your app. Therefore, understanding iOS infrastructure and how they work can be helpful for writing apps.

Main function entry

All the portals of C-based apps are main functions, but iOS apps are a little different. The difference is that you don't have to write your own functions for iOS apps main , and when you use Xcode to create a project, you've already provided it. Unless you have special circumstances, you should not modify the function implementations provided by Xcode main . The sample code is as follows:

#import <UIKit/UIKit.h>#import "AppDelegate.h"int main(int argc, char * argv[]){ @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }

The above example code has a very important function uiapplicationmain, which is mainly to create several core objects of the app to handle the following procedures:

    1. Loading the Storyboard user interface from available files
    2. Call the AppDelegate custom code to do some initialization settings
    3. Put apps Main Run Loop in the environment to respond to and handle events generated by user interaction
Architecture of the application

iOS apps follow the schema of Model-view-controller , Model is responsible for storing data and processing business logic, View Responsible for displaying the data and interacting with the user, Controller is the intermediary between the two, coordinating Model and View to collaborate with each other. Their communication rules are as follows:

    1. ControllerAble to access Model and View , Model and View cannot access to each other


      MVC communication-reference from Stanford university.png
    2. When an View event is generated with a user interaction, the method is used target-action to handle


      MVC communication-reference from Stanford university.png
    3. When you View need to handle some special UI logic or get a data source, pass it on delegate or give it a data source way Controller to handle


      MVC communication-reference from Stanford university.png
    4. ModelCannot communicate directly with Controller , when Model there is data update, can pass Notification or KVO (Key Value Observing) to notify Controller updateView


      MVC communication-reference from Stanford university.png

After understanding the MVC design Patterns of iOS, we never know what the key objects are in the MVC model and what are the main responsibilities of the iOS application?


The Structure of an app.png
  • UIApplication Object
    Events generated when a user interacts with an iOS device (Multitouch events,motion event,remote control event) are assigned UIApplication to the object to distribute control objects( Uicontrol) corresponds to the target objects to handle and manage the entire event loop, while some of the important events entrusted to the app runtime are app delegate handled.

  • APP Delegate Objects
    App delegateThe object follows UIApplicationDelegate the protocol, responds to important events when the app is running (app Launcher, app out of memory, app stops, switches to another app, and cuts back to the app), primarily for apps that initialize some important data structures at startup, such as initializing UIWindow , setting some properties, Add to Window rootViewController .

  • View Controller Object
    View ControllerOne view property is the root viewin the view hierarchy, and you can add sub-view to build complex View;controller some viewDidLoad , viewWillAppear and so on, to manage the view's life cycle; UIResponder, all of the user events are also responded to and handled.

  • Documents and Data Model objects
    The data model object is primarily used to store the information. For example, when the app searches for a switch address, it has a history search address history, and when the app is next launched, it reads and displays the search address history.
    The document object (inherited Uidocument) is used to manage some or all of the data Model objects. Document objects are not required, but provide a convenient way to group data that is part of a single file or multiple files.

  • UIWindow Object
    UIWindowThe object is at the topmost level in the view hierarchy, acting as a basic container without displaying content, and adding a content view to the window if you want to display it.
    It is also inherited UIResponder , so it will also respond to and handle user events.

  • View, control, Layer object
    Viewobjects can manage The view hierarchy using methods such as Addsubview and Removefromsuperview, and use methods such as layoutifneeded and Setneedslayout to lay out View hierarchy, you can override the DrawRect method or use the Layer property to construct complex graphical appearances and animations when you find that the system provides a view that does not meet your desired appearance requirements. Also, UIView also inherits Uiresponder, so it can handle user events as well.
    ControlAn object is typically a view that handles a particular type of user interaction, often with a button, switch, Text field, and so on.
    In addition to using View and Control to build the view hierarchy to influence the app's appearance, you can use the objects of the core animation framework Layer to render the view appearance and build complex animations.

Main Run Loop

The main run loop of an iOS application is to handle all user-related events. UIApplicationThe object sets the main run loop at startup and uses it to handle events and update the view-based interface. As its name implies, main run loop is the main thread running in the application. This ensures that the events associated with receiving the user are processed in an orderly manner .

Shows how the schema and user events for main run loop are ultimately handled by the application. When the user interacts with the device, the system generates an event associated with the interaction, which is then distributed by the application's Uikit through a special port. The application puts events into the queue and then distributes them to main run loop one by one. The UIApplication object is the first object that receives the event and then decides what to do with it. A touch event is typically distributed to the main Window object, which is then distributed to the touch view inturn. The Receive event object path for other events may be a little different.


Main Run Loop from Apple Document

Most events are distributed by using the main run loop, but some are not. Some events are sent to a delegate object or passed to the block you provide. To learn more about how to handle most types of events, including touch, remote control, motion, accelerometer, and gyroscopic events, see Event Handle Guide for IOS.

Application state and multi-tasking

Sometimes the system switches from one state of the app to another to respond to events that occur in the system. For example, when a user presses the home key, a phone break, or another interrupt occurs, the currently running application switches state to respond. The status of the application is as follows:


APP state from Apple Document
    • Not Running:app is not running yet
    • Inactive:app running on foreground but not receiving events
    • Active:app running in foreground and receiving events
    • Background: Running in Background and executing code
    • Suspended: Running in background but no code execution

Most state transitions invoke delegate the object's corresponding method to respond to the app's state change. delegateall of the methods of the object are summarized below, which you might use when the app state transitions.

    • application:willFinishLaunchingWithOptions:-This method is your first chance to execute code at startup
    • application:didFinishLaunchingWithOptions:-This method allows you to perform the final initialization operation before displaying the app to the user
    • applicationDidBecomeActive:-What you need to do after the app has switched to the active state
    • applicationWillResignActive:-What you need to do when the app is going to switch from the foreground to the background
    • applicationDidEnterBackground:-What you need to do after the app has entered the background
    • applicationWillEnterForeground:-the app is going to switch from the background to what the foreground needs to do, but the app is not still active
    • applicationWillTerminate:-Actions to be performed when the app is about to end

Now let's talk about the way the app starts, switches back and forth between the app and lock screen, and calls the methods that correspond to which delegate objects:

  • App Launch and Active/inactive


    Launch and active/inactive from Apple WWDC Session


    , when the app starts, the method is called when the state is first not running switched to the inactive state, and application:didFinishLaunchingWithOptions: then by the state to the inactive active state, at which point the method is called applicationDidBecomeActive: .


    Launch and Active/inactive 2 from Apple WWDC Session

    When the app is interrupted, the active state is switched to the inactive state, and the method is called applicationWillResignActive: .

  • Switch apps back and forth


    Switch from a app from Apple WWDC Session


    , when switching to another app, the method is called when the state is active switched to, and the inactive method is applicationWillResignActive: then switched from the inactive state to the running state at this time applicationDidEnterBackground: .


    Switch to a app from Apple WWDC Session


    When you switch back to the original app, the running state is switched to the inactive state, and the method is called, and then the method is called by the state to the state applicationWillEnterForeground: inactive active applicationDidBecomeActive: .

  • Lock screen


    Device lock from Apple WWDC Session


    How to do this, when the phone locks the screen, it is called by the state, and then the method is called when the state active inactive applicationWillResignActive: inactive switches to the running state applicationDidEnterBackground: .

    For more information about app State switching and app delegate which methods to call, watch the Session_320__adopting_multitasking_in_your_app video of the WWDC session.

Termination of the application

The system is often launched for other apps because memory is not enough to reclaim memory and eventually need to terminate the application, but sometimes it will be due to the app for a long time to respond and terminate. If the app is running in the background and there is no pause, the system is called before the application terminates to applicationWillTerminate: save some of the user's important data for the next startup to revert to the original state of the app.

Summarize

This article summarizes what key objects are involved in iOS applications from start to finish, and when a user interacts with the system to generate an event, the system uses main run loop to manage the event loop, deciding which objects are to be handed over to the system and how to handle the event. And when the app launches, switches back and forth between the app and the lock screen, how the app's state switches and calls the corresponding app delegate objects to handle.



Wen/sam_lau (author of Jane's book)
Original link: http://www.jianshu.com/p/aa50e5350852
Copyright belongs to the author, please contact the author for authorization.

The life cycle of an iOS app

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.