In the Apple developer docs chapter "There and Back Again" the watch app’s App is written like this:
@main
struct MyWatchApp: App {
@WKExtensionDelegateAdaptor(ExtensionDelegate.self) var extensionDelegate
@SceneBuilder var body: some Scene {
WindowGroup {
NavigationView {
ContentView()
}
}
}
}
Unfortunately I get a purple runtime warning on my var declaration that says
@WKExtensionDelegateAdaptor should only be used within an extension based process
There must be something in Xcode that explicitly defines the App structure as "extension-based" but I can’t find it!
Edit: More clarification… I am trying to handle the special method that gets called after you run the HealthKit method startWatchApp(with:completion:)
The special method for watch extensions is func handle(_ workoutConfiguration: HKWorkoutConfiguration)
I cannot seem to find a way to link this function on the new App
structure for watch apps.
2
Answers
Ok, I found it. The solution is to simply replace
WKExtensionDelegate
with the newWKApplicationDelegate
!You can try this solution, it is simple and universal.
Then access this current instance like below, because it is Singleton, you can access it in the entire project scope.