是否有可能从我的应用程序发送消息到我的watchOS分机,所以我ex.可以更新我的手表UI吗?
我使用WKInterfaceController.openParentApplication将手表扩展中的信息发送到应用程序。但是我该如何做相反的事情呢--从App到Watch扩展?
发布于 2018-05-08 13:38:24
观看OS 4+
更新@Philip Answer您需要在设置会话后完成
您需要使用replyHandler方法将数据从iPhone获取到WatchOS应用程序扩展,如下所示
WCSession.default.sendMessageData(Data(), replyHandler: { (data) in
let deviceId = String(data: data, encoding: .utf8)
print("DEVICE ID : \(deviceId)")
DataModel.shared.deviceId = deviceId
}, errorHandler: nil)这将根据您的要求从Watch App扩展中调用。
在主应用代理中,你需要实现WatchKit会话代理
// WATCH OS MESSAGE RECIEVE WITH REPLY HANDLER
func session(_ session: WCSession, didReceiveMessageData messageData: Data, replyHandler: @escaping (Data) -> Void) {
print("SESSION MESSSAGE DATA: \(messageData)")
if let deviceId = UIDevice.current.identifierForVendor?.uuidString,
let data = deviceId.data(using: .utf8) {
print("REPLY HANDLER MESSSAGE DATA: \(data)")
replyHandler(data)
}
}在replyHandler中,您可以传递Data格式的信息。
https://stackoverflow.com/questions/31102866
复制相似问题