Swift 如何从父iOS应用程序打开手表应用程序?
我需要从我的父iOS应用程序打开我的手表扩展。Swift 如何从父iOS应用程序打开手表应用程序?,swift,apple-watch,watchos-3,Swift,Apple Watch,Watchos 3,我需要从我的父iOS应用程序打开我的手表扩展。 我在Nike+Run Club应用程序中看到了类似的功能。也就是说,当用户点击父应用程序中的开始按钮时,将立即打开手表套件扩展。正如@abjurato所说,您只能在“训练模式”下启动它 这是通过HKHealthStore的StartWatchap(with:completion:)方法完成的。iOS应用程序配置训练,将其发送到手表,手表可以启动或不启动,但这只能在iOS应用程序处于前台时完成。不幸的是,这不会唤醒手表。一旦手表醒来,手表应用程序就会
我在Nike+Run Club应用程序中看到了类似的功能。也就是说,当用户点击父应用程序中的开始按钮时,将立即打开手表套件扩展。正如@abjurato所说,您只能在“训练模式”下启动它
这是通过HKHealthStore的StartWatchap(with:completion:)方法完成的。iOS应用程序配置训练,将其发送到手表,手表可以启动或不启动,但这只能在iOS应用程序处于前台时完成。不幸的是,这不会唤醒手表。一旦手表醒来,手表应用程序就会立即被买到前台,从这个意义上讲,文档是非常误导的。
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
}
}