如何从父iOS应用程序打开手表应用程序?

sam*_*353 4 swift apple-watch watchos-3

我需要从我的父iOS应用程序打开我的手表扩展程序.我在Nike + Run Club应用程序中看到了类似的功能.即,当用户点击父应用程序中的开始按钮时,将立即打开监视工具包扩展.

paw*_*oon 8

正如@abjurato所说,你只能以"锻炼模式"启动它

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
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这不会唤醒手表。一旦手表唤醒手表应用程序立即购买到前台,从这个意义上说,文档非常具有误导性。 (2认同)