首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WatchConnectivity transferFile的问题(观看-> iphone)

WatchConnectivity transferFile的问题(观看-> iphone)
EN

Stack Overflow用户
提问于 2020-01-03 16:14:04
回答 1查看 579关注 0票数 4

我想使用WatchConnectivity和一个设置WCSession将在手表上创建的文件发送到iOS配套应用程序,但它无法连接到iPhone。当我使用发送包含字典的消息时,数据确实到达了iPhone。

AppDelegate和ExtensionDelegate都使用NotificationCenter与ViewController和ExtensionController通信。为了简单起见,它们被省略了,但通知工作得很好。

iOS的AppDelegate.swift

代码语言:javascript
复制
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

代码语言:javascript
复制
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发送文件之后,如您所见,我检查了WCSessionoutstandingFileTransfershasContentPending属性,它们表明文件应该已经被传输,但是我的AppDelegate扩展的:didReceive:方法没有被调用。同样,当我使用sendMessage时,会按预期调用AppDelegate的:didReceiveMessage:

我遗漏了什么?

注意:我试过运行这个以不同通信方法为特色的Apple's demo project,我体验到了同样的事情: transferFile不会在对等物上触发任何东西。

EN

回答 1

Stack Overflow用户

发布于 2020-05-08 01:14:18

这似乎是iOS 13/WatchOS6模拟器的一个错误。我曾尝试将Xcode模拟器更改为iOS 12和watchOS 5,这在this thread中是如何建议的,但问题消失了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59575166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档