App-Life Cycle

  • UIApplication object
    • The UIapplication object manages the event loop and other high-level app behaviors. It also reports key app transitions and some special events (such as incoming push notifications) to its delegate, which is a custom object you define. The UIApplication object is treated as a singleton, so you do not subclass it and it can be used anywhere in your application
  • App delegate
    • The app delegate is the heart of your custom code. This object works in tandem with the UIApplcation object to handle app initialization, state transitions, and many high-level app events
    • This object is also the only one guaranteed to be present in every app, so it is often used to set up the app’s initial data strucutres.
  • The Main Run Loop
    • An app’s run loop processes all user-related events. The Uiapplication object sets up the main run loop at lauch time and uses it to process events and handle updates to view-based onterfaces. As the name suggests, the main run loop executes on the app’s main thread. This behavior ensures that user-related events are processed serially in the order in which they were received.
    • Events are queued internally by the app and dispatched one-by-one to the main run loop for execution. The UIApplication object is the first object to receive the event and make the decision about what need to be done. A touch event is usually dispatched to the main window object, which in turn dispatches it to the view in which the touch occurred.
  • Scene based life cycle events
    • A scene represents one instance of you app’s UI Running on a device
    • The user can create multiple scenes for each app, and show and hide them seperatly
    • Because each scene has its own life cycle, each can be in a different state of execution
    • When the user or system requests a new scene for you app, UIKit creates it and puts it in the unattached state. User-requested scenes move quickly to the foreground, where they appear on screen. A system-requested scene typically moves to the background so that it can process an event. For example, the system might launch the scene in the background to process a location event. When the user dismisses your app’s UI, UIkit moves the associated scene to the background or suspended scene at any time to reclaim its resouces, returning that scene to the unattached state.
    • Use scene transitions to perform the following tasks:
      • When UIKit connects a scene to your app, configure your scene’s initial UI and load the data scene need.
      • When transitioning to the foreground-active state, configure your UI and prepare to interact with the user.
      • Upon leaving the foreground-active state, save data and quiet your app’s behavior.
      • Upon entering the background state, finish crucial tasks, free up as much memory as possible, and prepare for your app snapshot.
      • At scene disconnection, clean up any shared resources associated with the scene
      • In addition to scene-related events, you must also respond to the launch of your app using your UIapplicationDelegate object.
  • App-Based Life-Cycle Events
    • After launch, the system puts the app in the inactive or background state, depending on whether the UI is about to appear on screen. When launching to the foreground, the system transiitons the app to the active state automatically. After that the state fluctuates active and background until the app terminates
  • Use app transitions to perform the following tasks:
    • At launch, initialize your app’s data strucutres and UI.
    • At activation, finish configuring your UI and prepare to interact with the user.
    • Upon deactivation, save data and quiet your apps behavior
    • Upon entering the background state, finish crucial tasks, free up as much memory as possible for your app snapshot
    • At termination stop all work immediately and release and shared resources.

Apple:

  • The current state of your app determines what it can and cannot do at any time. For example, a foreground app has the user’s attention, so it has priority over system resources, including the CPU. By contrast, a background app must so as little work as possible, and preferably nothing, because it is offscreen. As your app changes from state to state, you must adjust its behavior accordingly
  • When your app’s state changes, UIKit notifies you by calling methods of the appropriate delegate object
    • In iOS 13 and later, use UIScenceDelegate objects to respond to life-cylce events in a scene-based app
    • In iOS 12 and earlier, use the UIApplicationDelegate object to respond to life-cycle events
  • When the user taps your app icon, Springboard the part of the OS that operates the home screen of iOS, launches your app.
  • Your app and the shared libraries it need to execute, will be loaded into the memory, while springboard animates your app’s launch screen.
  • States:
    • Not Running
      • Either the application has not started yet or was running and has been terminated by the system
  • In Active
    • An application is running in the Foreground but is not receiving any events. This could happen in the case a Call or Message is received, An application could also stay in this state while in transition to a different state
    • Active
      • An application is running in the Foreground and receiving the events. This is the normal mode for the Foregournd apps. The only way to go to or from the Active state is through the Inactive state. User normally interacts with UI, and can see the response/result for user actions.
    • Background
      • An application is running in the background and executing the code. Freshly launching apps directly enter into In-Action state and then to Active states. Apps that are suspended, will come back to this background state, and then transition to In-Active -> Acrive states. In addition, an application being launched directly into the background enters this state instead of the inactive state.
    • Suspended states
      • An application is in the background but is not executing the code. The system moves the application to this state automatically and does not notify. In case of low memory, the system may purge suspended applications without notice to make free space for the foreground application. Usually after 5 sec spent in the background, apps will transition to Suspended state, but we can extend the time if the app needs it.
    • Apple – the iOS operating system manages the application states, but the app is responsible for handling user-experience through these states transitions
    • UIApplication
      • Object defines some methods which are called or will be responded to some of the above states which are most important, to let us work on those transitions states regarding our app functionalities.

App transition condition         App execution State   Delegate Called

 Device power off                   suspended                 none

 Device power off                   background                applicationWillTerminate

 Device power off                   background                applicatonDidEnterBackground

                                                                                     applicationWillTerminate

 Force quit from                       suspended                 none

 Multitasking UI

 Force quit from                      background                applicationWillTerminate

 Multitasking UI         

 Force quit from                      foreground                 applicationDidEnterBackground,

 Multitasking UI                                                          applicationWillTerminate

 Low memory/dropped          suspended                 none

 By iOS

 Low memory/dropped.          Background                applicationWillTerminate

 By iOS

  • Launch time
    • Application:willFinishLaunchingWithOptions – This method is called after your application has been launched successfully. It is the first method from out app delegate, which will be called. You can execute your code if the launch was successful
    • Application:didFinishLaunchingWithOption – This method is called before the app’s window is displayed. You can finalise your interface and can provide the rootViewControllers to the window
  • Transitioning to foreground
    • applicationDidBecomeAcrive – This method is either called to let your app know that it moved from the inactive to active state or your app was launched by the user or the system or in case user ignores an interruption (such as an incoming phone call or SMS message) that sent the application temporarily to the inactive state. You should use this method to restart any tasks that were paused (or not yet started) while the app was inactive
    • applicationDidEnterBackround – This method is called to let the app know that it is not running in the foreground. You have approximately five second to perform any tasks and return back. In case you need additional time, you can request additional execution time from the system calling beginBackgroundTasks(expierationHandler:) If the method does not return before time runs out your app is terminated and purged from memory
    • applicationWIllEnterForeground – This method is called as part of the transition form the background to the active state. You should use this to undo any change you made to your app upon entering the background. ApplicationDidBecomeActive method is called soon after this method has finished its execution which then moves the app from the inactive to the active state.
  • Transitioning to inactive state
    • applicationWillResignActive – This method is called to let your app know that it is about to move from active to inactive state. This can happen in case of any interruptions (such as an incoming phone call or SMS message or Calendar alerts) or when the user quits the app. You should use this method to pause any ongoing tasks or disable timers etc.
  • Termination
    • applicationWillTerminate – This method is called to let you know that your app is about to terminate. You should use this method to perform any final clean-up task. You have approximately 5 seconds to perfrom any tasks and return back. If the method does not return before time expires, the system may kill the process altogether. This method may be called in situations where the app is running in the background (not suspended) and the system need to terminate it for some reason. You should not wait applicationWillTerminate to be called in order to save your data. There are some cases when applicatoinWillTerminate won’t be called before the app termination. For example, the sytem will not call applicationWIllTerminate when the device reboots.

Managing Your App’s Life Cycle

https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle

The iOS Application LifeCycle:

https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle

iOS App Life Cycle

https://medium.com/@neroxiao/ios-app-life-cycle-ec1b31cee9dc