In an iOS application, you can obtain a reference to the shared app delegate by:
Swift:let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
Objective-C:AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
In a WatchKit2 App Extension there is a similar App Delegate and I want to obtain a reference to it in a view controller to access shared resources in the app, such as the ManagedObjectModel and PersistentStoreCoordinator for the Core Data stack, that I have initialised in the App Delegate.
However, UIApplication.sharedApplication().delegate as! AppDelegate
reports the error,
Use of unresolved identifier 'UIApplication'
How can I access the app delegate in a WatchKit2 App Extension?
The WKExtension
class in WatchOS 2 automatically provides a single extension object for each extension, to manage behaviors that are shared among all of your app’s interface controllers. The Apple Documentation notes that you "use the extension object to perform app-level tasks such as opening URLs and getting the root interface controller of your app."
Just like in iOS, in your WatchKit App Extension you are providing your own delegate object, the one you are trying to reference. This is automatically assigned to the delegate property of the WKExtension object, and can be accessed using a similar method to that used to access the UIApplication delegate in iOS:
Swift:let delegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
Objective-C:WKExtensionDelegate *delegate = [[WKExtension sharedExtension] delegate];
The Apple documentation of the WKExtension Class provides further information about capabilities.
Going deeper:
WatchKit App Extensions do not under all circumstances have to provide a WKExtensionDelegate. As the WKExtensionDelegate documentation from Apple notes, "You provide the delegate object and use it to manage lifecycle events in your extension. Providing a delegate object is required if your extension supports actionable notifications or Handoff behaviours."
You will know whether your WatchKit App Extension has a delegate object, and if it does, there will be no point in your App Extension lifecycle where you are trying to access that App Delegate where it will not exist. Therefore, while the WKExtension.sharedExtension().delegate
is an optional (WatchKit App Extensions could exist where no delegate has been set) it is safe to use as!
to force cast the return to be non-optional in the example above, given the developer knows they have set a WKExtensionDelegate in their App Extension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With