2017-02-16 94 views
1

我想從iOS發送圖像到watchOS,並且出現錯誤。WCSession sendMessageData在watchOS中不工作3

sendMessageData getting error: Error Domain=WCErrorDomain Code=7014 "Payload could not be delivered." UserInfo={NSLocalizedDescription=Payload could not be delivered.

我的視圖控制器類

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

我想你需要嘗試以下協議提到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) 
} 
相關問題