WatchConnectivity transferFile问题(手表-> iphone)

问题描述 投票:2回答:1

我想使用WatchConnectivity和设置WCSession将在手表上创建的文件发送到iOS伴侣应用,但无法通过iPhone。当我使用发送包含词典的消息代替时,数据确实到达了iPhone。

AppDelegate和ExtensionDelegate都使用NotificationCenter与ViewController和ExtensionController进行通信。为了简单起见,省略了它们,但是通知工作正常。

iOS's 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不会触发对方的任何操作。

watchkit apple-watch watchconnectivity
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.