Sync data between iOS and watchOS

Viewed 456

I am writing a relatively simple application to sync "in real time" a UIImage on the iPhone (similar to a drawing application) with a watchImage on the watch. Pretty simple case. I am using WatchConnectivity and simple message transfer between the phone and the watch, and I am able to send my image from the iPhone to the watch.

On the phone, I am using the simple

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    ...
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    ...
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    ...
    sendDataToWatch(image: picture.image!)
    }
func sendDataToWatch(image: UIImage) {
    if WCSession.isSupported() {
        let session = WCSession.default
        if session.isReachable {
            let message = ["imageData":image.jpegData(compressionQuality: 0.1)]
            session.sendMessage(message as [String : Any], replyHandler: nil, errorHandler: { (error:Error) in print("ERROR: \(error.localizedDescription)") })
        } else {
            // watch is not reachable         
        }
    }
}

On the watch :

func processMessage(_ message: [String : Any]) {
    if let imageData = message["imageData"] as? Data {
        let image = UIImage(data: imageData)
        self.watchImage.setImage(image)
    }
}

Everything works as expected : as long as the watch app is "active", I receive my drawing in real time on the watch.

My problem is when the watch goes "idle", for example with the wrist movement, by letting my arm down. The watch screen goes black, and the app enters "Frontapp" background, according to the documentation https://developer.apple.com/documentation/watchkit/working_with_the_watchos_app_life_cycle

If I am drawing on my iPhone screen while the watch is "idle", I don't receive any picture. I have 2 questions: - is there a notification or a callback that is triggered when the watch app goes back to normal state (visible), by rising my wrist? In that case I can ask the phone to send me an "updated" picture. If not, what is the best way to ensure phone and watch content are "always" synced?

Thanks for your help!

0 Answers
Related