重新启动Apple Watch WCSession'isRechable'后,每次都会返回false

时间:2019-10-22 18:50:23

标签: ios swift watchkit apple-watch watchconnectivity

我想在watch app中显示来自Firebase实时数据库的数据。 我有一种方法可以从iOS父级App中的Firebase获取数据。 我从手表应用程序向iOS父应用程序发送消息,iOS父应用程序将从Firebase中获取数据,并使用该数据回复手表应用程序。

在iOS父级应用中,如下激活了WCSession,

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    // Watch Connectivity setup
    if WCSession.isSupported() {
        let session = WCSession.default
        session.delegate = self
        session.activate()
    }

    return true
}

在Watch Extension中的ExtensionDelegate中以相同方式激活会话,

ExtensionDelegate.swift

func applicationDidFinishLaunching() {
    // Perform any final initialization of your application.

    if WCSession.isSupported() {
        let session = WCSession.default
        session.delegate = self
        session.activate()
    }
}

现在InterfaceController类中的消息已发送到iOS父应用程序,并获得了答复数据。

InterfaceController.swift

override func awake(withContext context: Any?) {
    super.awake(withContext: context)
    getData()
}

func getData() {
    if WCSession.default.isReachable {
        session.sendMessage(["Request": "GetData"], replyHandler: { (dictReply) in
            print(dictReply)
        }) { (error) in
            print(error)
        }
    } else {
        let action = WKAlertAction(title: "Ok", style: .cancel) {

        }
        self.presentAlert(withTitle: "Error", message: "iPhone is not reachable, try reconnecting watch with iPhone and try again.", preferredStyle: .alert, actions: [action])
    }
}

在iOS父应用中处理了收到的消息,并以字典的形式发送了回复数据。

AppDelegate.swift

func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
    guard let request = message["Request"] as? String else {
        replyHandler([:])
        return
    }

    switch request {
    case "GetData":
        // Call method to get data from firebase realtime database and send response dictionary in replyHandler for demo purpose currently passed test data
        replyHandler(["test" : "Hello..!!!"])
    default:
        replyHandler([:])
    }
}

现在,第一次在设备中运行时,重新启动Apple Watch打开watch app后,它的工作正常,bt,并在警报“ iPhone无法连接,请尝试将手表与iPhone重新连接,然后再试一次”中给我错误。这意味着WCSession.default.isReachable每次都返回false。应用程序停留在此处,只是在Apple Watch上显示黑屏。

另外,如果我从xcode运行应用程序,则每次运行都可以。

我正在使用iPhone 8和Apple Watch系列1

0 个答案:

没有答案
相关问题