我想使用WatchConnectivity和一个设置WCSession将在手表上创建的文件发送到iOS配套应用程序,但它无法连接到iPhone。当我使用发送包含字典的消息时,数据确实到达了iPhone。
AppDelegate和ExtensionDelegate都使用NotificationCenter与ViewController和ExtensionController通信。为了简单起见,它们被省略了,但通知工作得很好。
iOS的AppDelegate.swift
extension AppDelegate: WCSessionDelegate {
// 1
func sessionDidBecomeInactive(_ session: WCSession) {
print("WC Session did become inactive")
}
// 2
func sessionDidDeactivate(_ session: WCSession) {
print("WC Session did deactivate")
WCSession.default.activate()
}
// 3
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if let error = error {
print("WC Session activation failed with error: \(error.localizedDescription)")
return
}
print("WC Session activated with state: \(activationState.rawValue)")
}
func setupWatchConnectivity() {
// 1
if WCSession.isSupported() {
// 2
let session = WCSession.default
// 3
session.delegate = self
// 4
session.activate()
}
}
func session(_ session: WCSession, didFinish fileTransfer: WCSessionFileTransfer, error: Error?) {
print("didFinish fileTransfer")
}
func session(_ session: WCSession, didReceive file: WCSessionFile) {
print("didReceive file")
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) { // 2
print("didReceiveMessage reached")
}
}在我的手表上:
ExtensionDelegate.swift
extension ExtensionDelegate: WCSessionDelegate {
func setupWatchConnectivity() {
if WCSession.isSupported() {
let session = WCSession.default
session.delegate = self
session.activate()
}
}
func session(_ session: WCSession, activationDidCompleteWith
activationState: WCSessionActivationState, error: Error?) {
if let error = error {
print("WC Session activation failed with error: " +
"\(error.localizedDescription)")
return
}
print("WC Session activated with state: " +
"\(activationState.rawValue)")
}
func sendFileToPhone(_ notification:Notification) {
// 1
if WCSession.isSupported() && WCSession.default.isReachable {
// 2
if let fileURL = notification.object as? URL {
print("Sending file for URL: \(fileURL.absoluteURL.absoluteString)")
WCSession.default.transferFile(fileURL, metadata: nil) // <- if I use sendMessage here stuff works...
}
}
}
func session(_ session: WCSession, didFinish fileTransfer: WCSessionFileTransfer, error: Error?) {
// handle filed transfer completion
print("File transfer complete")
print("Outstanding file transfers: \(WCSession.default.outstandingFileTransfers)")
print("Has content pending: \(WCSession.default.hasContentPending)")
}
func session(_session: WCSession, didFinishFileTransfer fileTransfer: WCSessionFileTransfer, error: NSError?) {
print("error: ", error as Any)
}
}在从Watch发送文件之后,如您所见,我检查了WCSession的outstandingFileTransfers和hasContentPending属性,它们表明文件应该已经被传输,但是我的AppDelegate扩展的:didReceive:方法没有被调用。同样,当我使用sendMessage时,会按预期调用AppDelegate的:didReceiveMessage:。
我遗漏了什么?
注意:我试过运行这个以不同通信方法为特色的Apple's demo project,我体验到了同样的事情: transferFile不会在对等物上触发任何东西。
发布于 2020-05-08 01:14:18
这似乎是iOS 13/WatchOS6模拟器的一个错误。我曾尝试将Xcode模拟器更改为iOS 12和watchOS 5,这在this thread中是如何建议的,但问题消失了。
https://stackoverflow.com/questions/59575166
复制相似问题