How to open a watch app from parent iOS app?
Asked Answered
P

1

6

I need to open my watch extension from my parent iOS app. I have seen a similar feature implemented in Nike+ Run Club app. ie, When the User taps on Start button in Parent app will open the watch kit extension instantly.

Poulson answered 27/3, 2017 at 9:9 Comment(1)
this is done via startWatchApp(with:completion:) method of HKHealthStore. iOS app configures workout, sends it to watch and watch can start it or not, but that can be done only while iOS app is foreground. developer.apple.com/reference/healthkit/hkhealthstore/…Unbowed
V
12

As @abjurato said, you can only launch it in a "workout mode"

import HealthKit
import WatchConnectivity
let healthStore = HKHealthStore()

func startWatchApp() {
    print("method called to open app ")

    getActiveWCSession { (wcSession) in
        print(wcSession.isComplicationEnabled, wcSession.isPaired)
        if wcSession.activationState == .activated && wcSession.isWatchAppInstalled {
            print("starting watch app")

            self.healthStore.startWatchApp(with: self.configuration, completion: { (success, error) in
                // Handle errors
            })
        }

        else{
            print("watch not active or not installed")
        }
    }

}

 func getActiveWCSession(completion: @escaping (WCSession)->Void) {
    guard WCSession.isSupported() else { return }

    let wcSession = WCSession.default()
    wcSession.delegate = self

    if wcSession.activationState == .activated {
        completion(wcSession)
    } else {
        wcSession.activate()
        wcSessionActivationCompletion = completion
    }
}
Vostok answered 1/6, 2017 at 12:28 Comment(1)
Unfortunately this will not wake the watch. Once the watch wakes up the watch app is immediately bought to the foreground, the documentation is very misleading in this sense.Mcmorris

© 2022 - 2024 — McMap. All rights reserved.