Recently, I developed a watch project, which requires mobile phone watch communication. Select WatchConnectivity to implement the functionality. I’ve never studied it before, so I’m going to do a demo.

The first thing that really got Ben’s goat (probably not too much) was using the simulator to run the code and discovering that the phone watch couldn’t communicate. The message sent by the mobile phone, the watch can not see, the watch sent, the mobile phone can not receive. This is more annoying, so Baidu, Biying, Google a string down found that must use the real machine debugging. When the real thing works, it works.

The Demo is ready, combined with its own business, custom data objects, transfer custom object list Balabala… And then tragedy. In wcsession. sendMessage, the message type is [String: Any], and user-defined data cannot be transmitted. Therefore, the list of custom objects can only be converted to the corresponding data format for sending. At this point, the data is reluctantly assembled, passed to the watch end, the watch end also can receive the data and display as expected. But the smile that I was about to smile was gone. The dictionary is unordered, and the contents displayed are irritating in different ways every time. It might have been a little irritating, but now it’s annoying. Don’t spray, after all, when learning grammar, practice so unfortunately forget. And then, I changed the way I wrote it, and I’m happily done. I had a chicken leg for dinner.

Debugging no problem happy closure plate. Then ios version update, watch also update prompt, more. I didn’t expect you to come. WCSession, WCErrorCodeNotReachable, WCSession, WCErrorCodeNotReachable, WCSession, WCErrorCodeNotReachable, WCSession WCErrorCodeNotReachable All kinds of change code, and then after a long time depressed for a long time, and version update, more over, the problem did not, with the original seal board code, did not change the version, running well.

If you think it’s over, then you’re too young to hear about ios not updating!! There are various updates in the future, and the code of my classmate can also run today, but the next update version may not work, so I decided to solve this problem completely. By the way, I got my second shot of COVID-19 vaccine yesterday, and I wonder what the problem is when I feel like I’m a dog. I did another demo, no business, pure test. 1. Try to send a message to your phone from your watch, without answering data, and it works every time. The code is as follows:

//watch
let message = ["message":"hello phone"]
        wcSession.sendMessage(message, replyHandler: nil) { error in
            print(error.localizedDescription)
        }

//phone
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
       print("message:\(message)")
    }

Copy the code

2, try to watch to send a message to the phone, need to answer the kind of data, failed every time. The code is as follows:

//watch
let message = ["query": true]

        wcSession.sendMessage(message) { reply in
            let data = reply["data"] as! String
            print("watch reply data:\(data)")

            DispatchQueue.main.async{ [self] in
                self.showMessageLabel.setText(data)
            }
        } errorHandler: { error in
            print(error.localizedDescription)
        }

//phone
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
        var reply = [String: Any]()
        
        reply["data"] = ["hi watch, nice to see you."]
        print("reply:\(reply)")
        
        replyHandler(reply)
    }
Copy the code

Since I succeeded every time without answering, I will initiate the request in this way, and then let the mobile phone receive the message to send a message to the watch, is it ok? Then… It was a predictable failure. Then I tried updateApplicationContext, same failure. However, I found that the watch always succeeds in sending messages to the mobile phone, while the mobile phone always fails to send messages to the watch. In the code, I also judged that the session activationState is always activated, and the session isReachable is always true.

I tried everything I could think of, so I went to the forum and posted a question.

But no one to reply ing finally saw this article, feel that called a kind

There are a lot of people down there who have the same problem as me, but they haven’t solved it either. By the end, I was relieved that it had nothing to do with the implementation code. Maybe it was something that apple changed in the update to WatchConnectivity, which is just my guess.

Finally, I really want to make fun of it, app will be on the shelves, and then this update users can not use, disturbing……