WCSession sendMessageData在watchOS 3中不起作用

时间:2017-02-16 09:11:57

标签: ios swift3 watch-os-3 wcsession

我正在尝试将图像从iOS发送到watchOS,但我遇到了错误。

  

sendMessageData获取错误:错误Domain = WCErrorDomain Code = 7014      "无法提供有效负载。"      UserInfo = {NSLocalizedDescription =无法传递Payload。

我在ViewController类中的代码

var session: WCSession?
    override func viewDidLoad() {
        super.viewDidLoad()
        session?.delegate = self
        if (WCSession.isSupported()) {
            session = WCSession.default()
            session?.delegate = self
            session?.activate()
        }

    }

     let image = UIImage(named: "img1")!
     let data = UIImagePNGRepresentation(image)
     session?.sendMessageData(data!, replyHandler: { (data) in
          print(data)
        }) { (error) in
            print(error)}
}

在InterfaceController类

  override func willActivate() {
        super.willActivate()
        if (WCSession.isSupported()) {
            session = WCSession.default()
            session.delegate = self
            session.activate()
        }
    }

 func session(_ session: WCSession, didReceiveMessageData messageData: Data) {
        guard let image = UIImage(data: messageData) else {
            return
        }
        print(image)
 }

谢谢,

1 个答案:

答案 0 :(得分:1)

我认为你需要尝试下面提到的here协议。

 func session(_ session: WCSession, didReceiveMessageData messageData: Data, replyHandler: @escaping (Data) -> Void) {

    guard let image = UIImage(data: messageData) else {
        return
    }

    print(image)
    imageSet.setImage(image)
}