0

I have create an application which is sending data to watch for showing. When the watch is screen is active then it sending data perfectly, but when watch sleeps then an error occurred that device is not active.

My question is that when the watch is active any how it will get that data which send via using WKSession sendMessage method from my iPhone?

Sumeet Mourya
  • 434
  • 1
  • 7
  • 23

1 Answers1

0

If the watch screen is off, the calling sendMessage on the iPhone won't work. You can only send data in real time when the watch screen is on. This is different than when you are using sendMessage from the watch to the iPhone (iPhone screen can be off). This is the block of code I use anytime I call sendMessage from my iPhone code:

// Send messages to any paired apple watch.
func tryWatchSendMessage(message: [String : AnyObject]) {
    if self.session != nil && self.session.paired && self.session.watchAppInstalled {
        self.session.sendMessage(message, replyHandler: nil) { (error) -> Void in
            // If the message failed to send, queue it up for future transfer
            self.session.transferUserInfo(message)
        }
    }
}

Then I setup the apple watch app to have the same handler if it gets the data via sendMessage or transferUserInfo.

lehn0058
  • 19,977
  • 15
  • 69
  • 109
  • I have already done these things, but If the Watch screen is turn off and I send something to watch from my iPhone then what happened with the data which I send that time wither that will be lost or queued for the process when the watch screen turn ON? – Sumeet Mourya Oct 21 '15 at 05:46
  • 1
    If the screen is off and the iPhone calls sendMessage, the call will fail and the data will not be delivered to the apple watch. In this case, the code block above handles this by calling transferUserInfo with the same data. This queues the data to be processed when the watch screen turns back on. On the watch, you will need to implement both session:didReceiveMessage (for when sendMessage succeeds) and session:didReceiveUserInfo (for when sendMessage fails and transferUserInfo succeeds). – lehn0058 Oct 21 '15 at 14:49