【问题标题】:Problem with WatchConnectivity transferFile (watch -> iphone)WatchConnectivity transferFile 问题(手表-> iphone)
【发布时间】:2020-04-21 19:35:54
【问题描述】:

我想使用 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)
    }
}

从手表发送文件后,我检查了outstandingFileTransfershasContentPendingWCSession 属性,它们表明文件应该已经传输,但是我的:didReceive: 方法我的 AppDelegate 扩展没有被调用。同样,当我使用 sendMessage 时,AppDelegate 的 :didReceiveMessage: 会按预期调用。

我错过了什么?

注意:我尝试运行具有不同通信方法的 Apple's demo project,但我遇到了同样的事情:transferFile 不会触发对应方的任何内容。

【问题讨论】:

  • 也被此问题阻止... Xcode 11.3 (11C29)。发送方说它确实完成了文件传输而没有任何错误,但接收方的-session:didReceiveFile: 从未调用...
  • 我目前面临同样的问题。已经有修复了吗?
  • Apple 根本不可能使用 Xcode 开发他们的应用程序。不可能为它制作应用程序

标签: watchkit apple-watch watchconnectivity


【解决方案1】:

这似乎是 iOS 13/watchOS 6 模拟器的错误。我尝试将 Xcode 模拟器更改为 iOS 12 和 watchOS 5,this thread 中的建议方式,问题消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多