Apple Watch Not Passing Data to iPhone - Swift

☆樱花仙子☆ 提交于 2019-12-07 07:29:22

I think you have messed up in the sendMessage(), I cannot work out the replyHandler syntax, and you miss the errorHandler: parameter.

Anyway, I've tried your code, and with a few changes it would work.

1). In InterfaceController, the sendPressed():

    var count = 0    
@IBAction func SendPressed() {
    //Send Data to iOS
    let msg = ["Count" : "\(count)"]

    if session.isReachable {
        session.sendMessage(msg, replyHandler: nil, errorHandler: { (error) -> Void in
            print("Error handler: \(error)")
        })
        count += 1
    }
}

I've added a count, since the message must vary for each call (to conserve battery), so you can now press the button several times in a row. And a check to verify that the host application is reachable.

2.) In the ViewController, remember to update the GUI on the main thread:

    func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
    DispatchQueue.main.async {
        self.lablel.text = "Message : \(message)"
    }
}

Otherwise the label will not update when you receive the data.

Let me know if it helps you!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!