我正在尝试将图像从 iOS 发送到 watchOS,但出现以下错误。
sendMessageData 出现错误:错误域 = WCErrorDomain 代码 = 7014 “无法传递有效负载。” UserInfo={NSLocalizedDescription=无法传送有效负载。
我在 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)
}
谢谢,