Starting with watchOS 2, we have an ExtensionDelegate object that is similar to UIApplicationDelegate (responds to application lifecycle events).
I want to get a link to the first Interface Controller object that will be displayed at startup in order to set a property on it (for example, pass a data storage object).
According to docs, the rootInterfaceController property on WKExtension returns the original controller:
The root interface of the controller is in the main storyboard of applications and has an object of the main entry point associated with it. WatchKit displays the root interface controller during startup, although the application may present another interface controller before starting the completion of the sequence.
So, I try the following in ExtensionDelegate :
func applicationDidFinishLaunching() { guard let initialController = WKExtension.sharedExtension().rootInterfaceController else { return } initialController.dataStore = DataStore() }
Although the correct interface controller is displayed, the rootInterfaceController is currently zero. Interestingly, if I request the same property in willActivate() my interface controller, the property will be set correctly.
In an iOS application, you can already get the root view controller in applicationDidFinishLaunching() , and I thought it should work the same for watchOS.
Is there a way to set properties on my interface controller before it is displayed (externally)? This is mistake?
Thanks a lot for your answer!
source share