errorHandler:否与WCErrorCodeDeliveryFailed-不确定我要去哪里

时间:2018-11-19 21:49:38

标签: ios swift watchkit watch-os watchconnectivity

建立连接后,我正在尝试将手机的UUID字符串发送到手表(用户无需执行任何操作)。我使用了本教程(https://medium.com/@vanessaforney/ios-development-watch-connectivity-32415d415854),但我一直收到“ WCSession _onqueue_notifyOfMessageError:messageID:withErrorHandler:] 21FB4ABE-D177-4689-AF50-62759283112C errorHandler:否,出现WCErrorCodeDeliveryFailed错误。。我不确定自己在做什么错。事先道歉,我会举一个MCVE / SSCCE示例,但是我不确定它如何在此工作。任何帮助将不胜感激!

这是ios应用程序端的WatchSessionManager:

class WatchSessionManager: NSObject, WCSessionDelegate {
    static let sharedManager = WatchSessionManager()
    var device_id = ""

    private override init() {
        super.init()
        session?.delegate = self
        session?.activate()
    }

    func setDeviceID(id: String) {
        device_id = id
    }

    private let session: WCSession? = WCSession.isSupported() ? WCSession.default : nil

    private var validSession: WCSession? {
        if let session = session, session.isPaired && session.isWatchAppInstalled{
            os_log("paired and reachable")
            return session
        }
        return nil
    }


    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        os_log("activationdidcompletewith")
        updateApplicationContext()
    }

    func sessionDidBecomeInactive(_ session: WCSession) {
    }

    func sessionDidDeactivate(_ session: WCSession) {
    }

    func startSession() {
        session?.delegate = self
        session?.activate()

        updateApplicationContext()
    }

    func updateApplicationContext() {
        let context = ["device_id" : device_id]
        do {
            Swift.print("trying to update application context")
            try WatchSessionManager.sharedManager.updateApplicationContext(applicationContext: context)
        } catch {
            Swift.print("error updating application context")
        }
    }
}

// Application Context
extension WatchSessionManager {
    func updateApplicationContext(applicationContext: [String : Any]) throws{
        if let session = validReachableSession {
            do {
                os_log("actually updating context")
                try session.updateApplicationContext(applicationContext)
            } catch let error {
                throw error
            }
        }
    }
}

extension WatchSessionManager {
    // Sender
    private var validReachableSession: WCSession? {
        if let session = validSession, session.isReachable {
            return session
        }
        return nil
    }

    // Receiver
    func session(session: WCSession, didReceiveMessage message: [String : Any],
                 replyHandler: ([String : Any]) -> Void, errorHandler: ((Error) -> Void)? = nil) {
        os_log("receiver")
        if message["device_id"] != nil {
            updateApplicationContext()
        }
    }
}

然后在我的AppDelegate.swift中,包括:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    WatchSessionManager.sharedManager.startSession()
    WatchSessionManager.sharedManager.updateApplicationContext()
    return true
}

这是我在手表侧的PhoneSessionManager:

import WatchConnectivity
import os.log

class PhoneSessionManager: NSObject, WCSessionDelegate {
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        requestApplicationContext()
    }

    static let sharedManager = PhoneSessionManager()

    private let session: WCSession? = WCSession.isSupported() ? WCSession.default : nil

    func startSession() {
        session?.delegate = self
        session?.activate()
    }

    func requestApplicationContext() {
        sendMessage(message: ["device_id": true as AnyObject], replyHandler: nil, errorHandler: nil)
    }

    func sessionReachabilityDidChange(_ session: WCSession) {
        requestApplicationContext()
    }

}

extension PhoneSessionManager {
    func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
        DispatchQueue.main.async(execute: {
            UserSummary.userSummary.updateFromContext(applicationContext: applicationContext)
        })
    }
}

extension PhoneSessionManager {
    private var validReachableSession: WCSession? {
        if let session = session, session.isReachable {
            return session
        }
        return nil
    }

    func sendMessage(message: [String : Any],replyHandler: (([String : Any]) -> Void)? = nil, errorHandler: ((Error) -> Void)? = nil) {
        validReachableSession?.sendMessage(message, replyHandler: replyHandler, errorHandler: errorHandler)
    }
}

然后在我的ExtensionDelegate.swift中,我有:

import WatchKit

class ExtensionDelegate: NSObject, WKExtensionDelegate {
    override init() {
        super.init()
        PhoneSessionManager.sharedManager.startSession()
    }

    func applicationDidFinishLaunching() {
        PhoneSessionManager.sharedManager.requestApplicationContext()
        // Perform any final initialization of your application.
    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用send message函数发送整数数组时遇到此错误。我建议您尝试sendMessage(message:[“ device_id”:“ test”],replyHandler:nil,errorHandler:nil)而不是将True作为AnyObject发送。如果这样对您有用,请尝试将true作为布尔值而不是AnyObject发送。