In an iOS application, you can obtain a reference to the shared app delegate by:
Swift:
let delegate = UIApplication.sharedApplication().delegate
extension ExtensionDelegate {
static var shared: ExtensionDelegate {
guard let delegate = WKExtension.shared().delegate as? ExtensionDelegate else {
fatalError("ExtensionDelegate")
}
return delegate
}
}
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.