Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using application:willFinishLaunchingWithOptions instead of application:didFinishLaunchingWithOptions:

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.

like image 453
Sandeep Singh Avatar asked Nov 24 '14 01:11

Sandeep Singh


People also ask

What happen if I return false in Didfinishlaunchingwithoptions?

If either method returns false , the URL is not handled. If you do not implement one of the methods, only the return value of the implemented method is considered.

What is an app delegate?

So an app delegate is an object that the application object can use to do certain things like display the first window or view when the app starts up, handle outside notifications or save data when the app goes into the background.


2 Answers

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.

like image 151
itsazzad Avatar answered Sep 19 '22 02:09

itsazzad


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

like image 20
Motti Shneor Avatar answered Sep 18 '22 02:09

Motti Shneor