13

I see Apple in default template use application:didFinishLaunchingWithOptions: to setup window in appDelegate class. My question is why don't use application:willFinishLaunchingWithOptions instead. Are there something which can only be done in application:didFinishLaunchingWithOptions.

Can I use application:willFinishLaunchingWithOptions to initialize rootViewController.

I still new in IOS, I can understand this might be a stupid question, but any help will be appreciated.

Sandeep Singh
  • 897
  • 2
  • 8
  • 20

3 Answers3

8

application:willFinishLaunchingWithOptions:

Tells the delegate that the launch process has begun but that state restoration has not yet occurred.

Vs

application:didFinishLaunchingWithOptions:

Tells the delegate that the launch process is almost done and the app is almost ready to run.

So the difference is clearly visible. For details you can follow UIApplicationDelegate

So application:willFinishLaunchingWithOptions is just the very previous event of application:didFinishLaunchingWithOptions

So after having the event application:willFinishLaunchingWithOptions, the next event application:didFinishLaunchingWithOptions will be fired.

So it will definitely depend on you needs but usually both are almost similar and most of the time you may not need to use both of those.

So in usual case you can use application:didFinishLaunchingWithOptions as its the latest event in the similar category.

itsazzad
  • 6,868
  • 7
  • 69
  • 89
  • 1
    that I understand, but can I use 'application:willFinishLaunchingWithOptions' to initialize stuff live UINavigationController. If yes, then is it good practice. – Sandeep Singh Nov 24 '14 at 01:39
  • In this case you should use application:didFinishLaunchingWithOptions as if you use application:willFinishLaunchingWithOptions then the application not yet have launched. UI jobs should be done afterwards. – itsazzad Nov 24 '14 at 01:49
  • 3
    What about Frameworks initializations like Parse or Google Analytics? – Iulian Onofrei Jan 23 '15 at 09:42
  • @IulianOnofrei You may follow https://developers.google.com/analytics/devguides/collection/ios/v3/ for google analytics. But I am not sure about 'Parse'. – itsazzad Jan 23 '15 at 15:20
  • 1
    One thing to notice is that, during CoreData migration, application:willFinishLaunchingWithOptions has the old version, then update step is performed, so that application:didFinishLaunchingWithOptions has updated CoreData version. – mixtly87 Apr 08 '16 at 06:26
  • you stated the obvious (from the docs, thanks) but not the difference and consequences. Specifically - is it OK or not to initialise viewControllers at the earlier event). My guess (not knowledge) is you should NOT initialise UI on the earlier - because state has NOT been restored, and so you don't know WHAT UI to initialise and present. – Motti Shneor Apr 02 '19 at 05:41
1

The docs helpfully dug by @itsazzad tell us that the time between these two events is used to "restore app state".

So it seems pointless to initialise view controllers at the earlier event - because state has not been yet restored - and you don't know which UI to present.

You may be re-launched after kill, and need to immediately skip to mid-work UI scenario.

So my answer is: only initialise UI view controllers on didFinishLaunchingWithOptions

Motti Shneor
  • 2,095
  • 1
  • 18
  • 24
  • Funny you answered this two weeks ago. Last week I posted a very related question at https://stackoverflow.com/questions/55550755/is-ios-state-restoration-possible-in-ios-library-could-not-find-a-storyboard In my case, I needed to start a framework with its own storyboard when the application launches. So to be "restorable", I now invoke the framework and assign the root view controller in application:willFinishLaunchingWithOptions, and it works well. The problem is that iOS can't find the framework's bundle during restoration of the storyboard/view controllers. Any help would be appreciated! – ScottyB Apr 17 '19 at 13:09
  • I got to mess with this difference for a completely different matter - I register my MacOS to receive AppleEvents from other Apps (and from LaunchServices, as soon as I'm launched, like the "Open Document" or "Quit" appleEvents) and I was missing events because I only registered at "didFinishLaunching"... anyway I think you should postpone your Framework's UI to AFTER the didFinishLaunching. Initialize other things earlier. Not the UI. – Motti Shneor Dec 14 '21 at 10:20
0

In other words willfinishlaunchingwithoptions is exclusive to restoration, while didfinishlaunchingwithoptions is when the whole process is complete.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Bogdan
  • 29
  • 6