Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a watch app from parent iOS app?

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.

like image 930
samad5353 Avatar asked Mar 27 '17 09:03

samad5353


1 Answers

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
    }
}
like image 140
pawisoon Avatar answered Sep 21 '22 12:09

pawisoon