swiftapple-watchwatchos-3

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.


Solution

  • 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
        }
    }