How can I guarantee registering for a notification happens in a view controller before didFinishLaunchingWithOptions?

StackOverflow https://stackoverflow.com/questions/17850601

Frage

Specifically, I want to register for a notification -before posted by the didFinishLaunchingWithOptions- in my main view controller so I can perform a task from the 'splash' (forgive me Ob-C nerds, forgot what its really called) screen without doing the ominous

[[[[[[UIApplication sharedApplication] delegate] window] rootViewController] topViewController] functionIwantToCallOnceAtTheStart];

Side note: I love the idea of having a 'global start whistle,' why is this not a default?

Logging didFinishLaunchingWithOptions and the root view controller's delegate methods results in

2013-07-25 02:02:46.532 Tests[918:c07] awakeFromNib
2013-07-25 02:02:46.535 Tests[918:c07] didFinishLaunchingWithOptions
2013-07-25 02:02:46.538 Tests[918:c07] viewDidLoad
2013-07-25 02:02:46.539 Tests[918:c07] viewWillAppear
2013-07-25 02:02:46.540 Tests[918:c07] viewWillLayoutSubviews
2013-07-25 02:02:46.543 Tests[918:c07] viewDidLayoutSubviews
2013-07-25 02:02:46.544 Tests[918:c07] viewWillLayoutSubviews
2013-07-25 02:02:46.545 Tests[918:c07] viewDidLayoutSubviews
2013-07-25 02:02:46.549 Tests[918:c07] viewDidAppear

but is it guaranteed that awakeFromNib will always come before didFinishLaunchingWithOptions, and on all iOS devices/versions?

This question has a helpful answer but my project is using storyboards, and I am unsure of the implications of setting the rootViewController manually with a storyboard.

Edit:

I originally asked this question because I wanted to have the launch image animate away. I realize there are better methods to do this, but I still want to use the "Application Started" notification to launch a network request to populate my main view controller (a UITableViewController) as soon as the app is started.

War es hilfreich?

Lösung

awakeFromNib is guaranteed to be called on the initial view controller of your storyboard before applicationDidFinishLaunching is called. Loading from the nib is part of the launch.

However, its not really clear from the question what you're trying to do. While the launch image is displayed, your view controller isn't really there, so what are you asking it to do? None of the UI objects will be present until it has loaded, so whatever task you're trying to do may not come under the responsibility of a view controller.

Andere Tipps

If you really really want to run some code, you can open up the main class. It's in your project under supporting files.

Also, this guide explains quite a bit on the appication startup sequence:

http://oleb.net/blog/2012/02/app-launch-sequence-ios-revisited/

The point of the launch image is to make your app appear to launch faster than it really does by putting a static image on screen as soon as possible. As long as your launch image looks exactly like whatever view your initial view controller shows, nobody will know the difference. If you want that initial image to animate away somehow, no problem:

  • Create an initial view controller that displays your image and does a custom transition to your next view controller.

  • If the image you're displaying covers the screen exactly, you can also use that image as the launch image. Otherwise, run the app and do a screen capture when the first view controller's view appears; make that screen shot your launch image.

  • Put some code in your -application:didFinishLaunchingWithOptions: method to start the transition from the initial view controller to the next one.

That should do it. Remember that the launch image is a sort of implementation detail -- the user isn't supposed to be aware of it as a separate thing, so it's natural to do what you're trying to do as a view controller transition instead of fooling around with the launch image itself.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top