skip to Main Content

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


  1. Chosen as BEST ANSWER

    Ok, I found it. The solution is to simply replace WKExtensionDelegate with the new WKApplicationDelegate!


  2. You can try this solution, it is simple and universal.

    import WatchKit
    
    class ExtensionDelegate: NSObject, WKExtensionDelegate {
        
        static var current: ExtensionDelegate? {
           return (WKExtension.shared().delegate as? ExtensionDelegate)
        }
    ...
    }
    
    

    Then access this current instance like below, because it is Singleton, you can access it in the entire project scope.

    ExtensionDelegate.current?.dosomething()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search