0

从事个人项目并尝试从 Apple Watch 接收内容。发送的数据很好,但手机或手表都没有回应。

在 AppDelegate 内部,我在内部启动了一个会话didFinishLaunchingWithOptions,它符合WCSessionDelegate.

    func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
    var replyValues = [String : Any]()
    switch message["command"] as! String {
    case "logAttempt":
        print("Log attempt")
        replyValues["response"] = "true"
    case "getData":
        print("Get course data")
        replyValues["response"] = "true"
    default:
        break
    }
    replyHandler(replyValues)

在 WatchKit 类内部,它也符合WCSessionDelegate并具有以下向手机发送消息的功能。

session.sendMessage(data, replyHandler: { replyHandler in
            print(replyHandler)
            if let response = replyHandler["response"] as? String {
                print("app: Response from app to watch: \(response)")
            } else {
                print("error: Somethings wrong!: \(replyHandler["response"])")
            }
        }) { error in
            print("Error: \(error.localizedDescription)")
        }

ExtensionDelegate.swift 文件也符合WCSessionDelegate并在applicationDidFinishLaunching. 努力让这个工作。

谢谢!

编辑:在 appDel 中设置手表:

fileprivate func setupWatch() {

    if WCSession.isSupported() {
        session = WCSession.default
        session!.delegate = self
        session!.activate()
    }

}
4

0 回答 0