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)
}
感謝,