11 Jan

iOS AppDelegate ViewController Method Order

iOS AppDelegate and ViewController have a number of methods that we use to accomplish many things.

But when is each method called?

Having just had to check on the order in one area, I thought it apt to write a quick blog post to show the order.

Please excuse the brevity of the post, as its sole purpose is to list a number of possible actions, along with which methods are called, and in what order.

AppDelegate method order

Launch app

didFinishLaunchingWithOptions (AppDelegate)

viewDidLoad (ViewController 1)

viewWillAppear (ViewController 1)

viewDidAppear (ViewController 1)

applicationDidBecomeActive (AppDelegate)

 

Switch to another view within the app

viewDidLoad (ViewController 2)

viewWillAppear (ViewController 2)

viewWillDisappear (ViewController 1)

viewDidDisappear (ViewController 1)

viewDidAppear (ViewController 2)

 

Switch back to the first view

viewWillAppear (ViewController 1)

viewWillDisappear (ViewController 2)

viewDidDisappear (ViewController 2)

viewDidAppear (ViewController 1)

 

Swipe down to show notifications

applicationWillResignActive (AppDelegate)

 

Swipe up to close notifications

applicationDidBecomeActive (AppDelegate)

 

Swipe up to show controls

applicationWillResignActive (AppDelegate)

 

Swipe down to close controls

applicationDidBecomeActive (AppDelegate)

 

Swipe down for notifications & tap a notification etc

applicationWillResignActive (AppDelegate)

applicationDidEnterBackground (AppDelegate)

 

Tap home button

applicationWillResignActive (AppDelegate)

applicationDidEnterBackground (AppDelegate)

 

Execute background fetch

performFetchWithCompletionHandler (AppDelegate)

 

Tap app

applicationWillEnterForeground (AppDelegate)

applicationDidBecomeActive (AppDelegate)

 

Thoughts

It is worth noting that viewWillDisappear / viewDidDisappear, and viewWillAppear / viewDidAppear are not called as the app goes from foreground to background, or background to foreground.

If you have a need to refresh screen components upon the app returning from background,  you should register a UIApplicationDidBecomeActiveNotification or UIApplicationWillEnterForegroundNotification in the viewDidLoad.