简体   繁体   中英

When does WCSession sendMessage fail to wake iOS app in background?

I'm using a WCSession object to send data from a watch app (watchOS 3.3 beta 4) back to the corresponding iOS app via sendMessage(_:replyHandler:errorHandler:) . According to the documentation from Apple :

Calling this method from your WatchKit extension while it is active and running wakes up the corresponding iOS app in the background and makes it reachable.

However, I find that if the isReachable property of the WCSession object is false before calling sendMessage , the message fails to send with this error: Error Domain=WCErrorDomain Code=7007 "WatchConnectivity session on paired device is not reachable." UserInfo={NSLocalizedDescription=WatchConnectivity session on paired device is not reachable.} Error Domain=WCErrorDomain Code=7007 "WatchConnectivity session on paired device is not reachable." UserInfo={NSLocalizedDescription=WatchConnectivity session on paired device is not reachable.}

The session is activated, so I believe this sendMessage call is supposed to make the iOS app reachable and then be sent, but this is not happening.

Under what circumstances does this happen, and are there any workarounds?

a) If you are using sendMessage(_, replyHandler:_, errorHandler:_) with non-nil reply handler, ios counterpart's WCSessionDelegate should have session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) method implemented, and message will be referred to as delivered once you'll call replyHandler(_:) inside this method. If this method is not implemented, message will not get response and you'll get timeout error.

b) Also, I've noticed that messages not be delivered while code in any of session(_ session: WCSession, didXXX: XXX) methods of WCSessionDelegate is executing. Looks like all of them are being executed in one serial background queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM