我需要通过父iOS应用打开手表扩展程序。
我已经在Nike + Run Club应用程序中实现了类似功能。即,当用户点击父应用程序中的“开始”按钮时,将立即打开手表套件扩展。

最佳答案

正如@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
    }
}

09-10 01:28
查看更多