0

I am trying to send a message from my iOS app to its companion Watch App. If the watch screen is ON, then everything works fine and I can see the messages. If the screen turns black, the watch is "not reachable" and my messages don't get printed.

iOS Code

// Invoking this function from viewDidLoad() of the view controller
func checkWatchConnectivityIsSupported() {
        if (WCSession.isSupported()) {
            print ("WC Session is supported")
            let session = WCSession.default
            session.delegate = self
            session.activate()
        }
    }

// sending messages on click of a button
func sendMessageToWatch(type: String, message: String) {
               
        print ("Sending message to watch \(type) \(message)")
        // send a message to the watch if it's reachable
        if (WCSession.default.isReachable) {
            print ("isReachable")
            // this is a meaningless message, but it's enough for our purposes
            let message = [type: message]
            // WCSession.default.sendMessage(message, replyHandler: nil)
            WCSession.default.sendMessage(message, replyHandler: nil, errorHandler: { (err) in
                print ("There was an error in sending message \(err)")
                            debugPrint(err)
                        })
           
        } else {
// This happens when the watch screen display goes off.
            print ("watch is not reachable")
        }
    }

WatchOS Code - InterfaceController.swift

// invoking this function from willActivate()
func checkIfWatchIsConnected() {
        if WCSession.isSupported() {
            let session = WCSession.default
            session.delegate = self
            session.activate()
        }
    }

// implementation of delegate methods
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {

    }
    
    func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
        print ("Message received in watch \(message)")
        WKInterfaceDevice().play(.click)
        let isStatusType = message["Status"] != nil
        if (isStatusType) {
            let text = message["Status"] as! String
            statusLabel.setText(text)
            return
        }               
    }
touchydeer36
  • 132
  • 9

1 Answers1

1

This is the expected behaviour.

WatchKit extension. The iOS device is within range, so communication can occur and the WatchKit extension is running in the foreground, or is running with a high priority in the background (for example, during a workout session or when a complication is loading its initial timeline data).

You use isReachable and sendMessage when live messaging is required. A watch app that provides a "remote control" for the active companion iOS app or an iOS app communicating with an active workout app on the watch are examples.

In order for live messaging to work the watch does, indeed, need to be awake.

You can use the updateApplicationContext and transferUserInfo methods to transfer data to your companion app when the watch isn't active. These transfers are queued and transferred opportunistically in order to improve battery life.

Paulw11
  • 108,386
  • 14
  • 159
  • 186