Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 重新启动Apple Watch WCSession';isRechable';每次都返回false_Ios_Swift_Watchkit_Apple Watch_Watchconnectivity - Fatal编程技术网

Ios 重新启动Apple Watch WCSession';isRechable';每次都返回false

Ios 重新启动Apple Watch WCSession';isRechable';每次都返回false,ios,swift,watchkit,apple-watch,watchconnectivity,Ios,Swift,Watchkit,Apple Watch,Watchconnectivity,我想在watch应用程序中显示firebase实时数据库中的数据。 我有一种从iOS父应用程序中的firebase获取数据的方法。 我正在从手表应用程序向iOS父应用程序发送消息,iOS父应用程序将从firebase获取数据,并用该数据回复手表应用程序 在iOS父应用程序激活的WCSession中,如下所示 AppDelegate.swift func application(_ application: UIApplication, didFinishLaunchingWithOptions

我想在watch应用程序中显示firebase实时数据库中的数据。 我有一种从iOS父应用程序中的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
}
func applicationDidFinishLaunching() {
    // Perform any final initialization of your application.

    if WCSession.isSupported() {
        let session = WCSession.default
        session.delegate = self
        session.activate()
    }
}
以相同的方式在扩展中激活会话在监视扩展中激活会话

斯威夫特

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
}
func applicationDidFinishLaunching() {
    // Perform any final initialization of your application.

    if WCSession.isSupported() {
        let session = WCSession.default
        session.delegate = self
        session.activate()
    }
}
现在,在InterfaceController类中,向iOS父应用程序发送消息,并在回复中获取数据

界面控制器

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后,bt打开watch应用程序并向我发出警告“iPhone无法访问,请尝试将手表与iPhone重新连接,然后重试。”这意味着WCSession.default.isReachable每次都返回false。应用程序卡在这里,只是在Apple watch上显示黑屏

另外,若我从xcode运行应用程序,那个么它每次都会运行

我正在使用iPhone8和Apple Watch系列1