Uiapplication delegate message and misunderstanding of applicationwillterminate trigger mechanism

Source: Internet
Author: User
Uiapplication delegate message prepared by: xuguoxing category: IOS, system, uikit Release Date:
Comment 6 comments
Off 1. uiapplication delegate Message Sequence

After ios4, multitasking is introduced. uiapplication runs in two modes: foreground (foreground) and background (background). When the app starts and switches between the frontend and background, the delegate method of uiapplicaton is called, describes delegate messages in various situations.

The corresponding PDF file.

Different delegate messages are sent according to different situations during the state conversion of uiapplication. The following describes the status conversion of uiapplication and the delegate messages to be sent.
First, the number of the delegate message involved in uiapplication status conversion is as follows:

  1. Application: didfinishlaunchingwitexceptions:
  2. Application: Openurl: sourceapplication: annotation:
  3. Applicationdidbecomeactive:
  4. Applicationwillresignactive:
  5. Applicationdidenterbackground:
  6. Applicationwillenterforeground:
  7. Applicationwillterminate:

The order in which the following delegate messages are sent is

  1. Started for the first time through springboard, the message order is 1-3
  2. The first time a message is started through a URL, the message order is 1-2-3
  3. Press the Home key during the operation, and the message order is 4-5.

    • If uiapplicationexitsonsuspend is set under ios4.0 Or info. plist, send the message again.
      7. Then the program is terminated;
    • Otherwise, enter the background running mode or suspend state based on the program settings.
  4. Press the lock screen key during running. The message order is the same as pressing the Home key. The difference is that all network connections (ios5.0 +) are closed at this time)
  5. If the message is interrupted by SMS or phone number during operation, Message 4 is sent.

    • If you select SMS or phone number, send message 5. The subsequent process is the same as pressing the Home Key.
    • If you ignore the SMS or phone number, send message 3 and return to the foreground running status.
  6. Double-click the Home Key to display the task switch. The message is sent.

    • If you select another program or click the Home Key to return to springboard, send message 5. The subsequent process is the same as pressing the Home Key.
    • If you double-click the Home key again to return to the app, send message 3 and return to the foreground running status.
  7. The message sequence is 6-3 when the node is started again through springboard or task switch.
    From the conversion process of the background, that is, the middle of the Message 6 and the third, the simulator 5.1displays default.png, but the screenshot to the background displayed on the real device.
  8. When the URL is started again, the message sequence is 6-2-3.
  9. When the program is in the background/suspend status, you double-click the Home key. When you delete this program in the task switch, the app will terminate immediately and no message will be sent.
  10. When the program is in the suspend State, if the memory is insufficient, the system will immediately terminate the program to recycle the memory. At this time, no messages will be sent.
2. Status transition broadcast notification

The above message will only be sent to appdelegate. If you want to monitor the status change of uiapplication in uiviewcontroller, you can listen to the following notifications:
Uiapplicationdidbecomeactivenotification (IOS
2.0 +)
Uiapplicationdidenterbackgroundnotification (IOS
4.0 +)
Uiapplicationwillenterforegroundnotification (IOS
4.0 +)
Uiapplicationwillresignactivenotification (IOS
2.0 +)
Uiapplicationwillterminatenotification (IOS
2.0 +)

Use the following code to obtain the running status of the current application.

[UIApplication sharedApplication].applicationState

Status includes
Uiapplicationstateactive
Uiapplicationstateinactive
Uiapplicationstatebackground

When an app is displayed in the background or returned from the background to the foreground when a uiviewcontroller is displayed, viewwilldisappear:, viewdiddisappear:, viewwillappear:, viewdidappear: series method, however, the view does have disappear and appear events.
In this case, you can register a listener for the above notifications in viewdidappear:, and handle the notifications when the app enters the background and returns from the background. In viewwilldisappear:, we can cancel listening to the preceding notifications, because if the current view is not displayed, no disappear or appear events occur, in this case, no special processing is required during the frontend/backend switch.
The following situations are required:

  • When viewing appear, you need to request data again;
  • When a view contains an animation, the animation stops when it enters the background mode and needs to be restarted when it is returned to the foreground;
  • ...
    3. corelocation update message

    If corelocation is used, when the program runs in background mode and the location is updated, special messages need to be processed. For details, see corelocation background update messaging.

    Refer:
    Understanding IOS 4 Backgrounding and delegate messaging
    Corelocation background update messaging
    Reload application data when the app comes into foreground?
    IOS default.png show every time when my app enter foreground from background
    IOS how to judge application is running foreground or background?
    IPhone app network connection disconnect after screen locking with new iOS
    Sdks 5.0
    Locking iPhone disconnects sockets on iOS 5 only
    Uiapplicationdelegate Protocol Reference
    Uiapplication class reference

    Reprinted-breeze Xu Lai, the blog http://www.winddisk.com/2012/08/29/uiapplication-delegate-messagin/


    It is helpful for processing when the program exits, but the statement in this article is incorrect. correct it.

    I. in the message sequence of uiapplication delegate 9. when the program is in the background/suspend status, you double-click the Home key. When you delete this program in the task switch, the app will terminate immediately and no message will be sent. This statement is incorrect. The situation should be differentiated. After ios4.0, the program can run in background because it supports multiple tasks. This option is enabled by default. A. If the program enables required background modes at the same time, such as APP plays audio, if the program is home to the background and playing music, you can manually/automatically abort the program to trigger applicationwillterminate. If you use a background task to process beginbackgroundtaskwithexpirationhandler, the same applies. B. If the app does not execute background tasks in the background, suspend will be entered. manual/automatic program suspension will not trigger applicationwillterminate. Conclusion: 1. applicationwillterminate will not be triggered when the suspend state is terminated:

    2. applicationwillterminate is triggered when the program is in the background state and is aborted when a background task is executed:

    Therefore, to save data and status when the program exits, it is best to process applicationdidenterbackground: And applicationwillterminate: examples.

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