Watch OS 应用程序无法与 iOS 应用程序连接 [英] Watch OS app not able to connect with iOS app

查看:76
本文介绍了Watch OS 应用程序无法与 iOS 应用程序连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的 Watch OS 应用程序与 iOS 应用程序连接并获取一些数据,但是当我尝试与 iOS 应用程序连接时出现以下错误:

I am trying to connect my Watch OS app with iOS app and fetch some data but I am getting following error when I try to connect with iOS app:

[WC] __28-[WCSession activateSession]_block_invoke_2 sessionReadyForInitialStateWithCompletionHandler 由于 NSXPCConnectionInterrupted 失败

[WC] __28-[WCSession activateSession]_block_invoke_2 sessionReadyForInitialStateWithCompletionHandler failed due to NSXPCConnectionInterrupted

__44-[WCSession updateApplicationContext:error:]_block_invoke 由于 WCErrorCodeSessionNotActivated 失败WatchConnectivity 会话尚未激活.

__44-[WCSession updateApplicationContext:error:]_block_invoke failed due to WCErrorCodeSessionNotActivated WatchConnectivity session has not been activated.

iOS 应用代码:

- (void) startSession{
if ([WCSession isSupported]) {
    self.session = [WCSession defaultSession];
    self.session.delegate = self;
    [self.session activateSession];
}

}

观看操作系统代码:

func startSession() {
    if(session.activationState != .activated){
        session.delegate = self
        session.activate()
    }
}
public func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?){
    print("activationDidCompleteWith")
    if activationState == WCSessionActivationState.activated {
        NSLog("Activated")
        if(WCSession.default().isReachable){

            do {
                try session.updateApplicationContext(
                    [WatchRequestKey : "updateData"]
                )
            }
            catch let error as NSError {
                print("\(error.localizedDescription)")
            }
        }
    }
    
    if activationState == WCSessionActivationState.inactive {
        NSLog("Inactive")
    }
    
    if activationState == WCSessionActivationState.notActivated {
        NSLog("NotActivated")
    }
}

iOS 应用在目标 C 中,手表应用在 swift 中

iOS app is in objective C and watch app is in swift

我首先执行我的 iOS 应用程序代码,它的委托方法运行,但是当我运行 watch os 应用程序时,它无法执行任何委托方法,并产生上述错误.

I execute my iOS app code first and it's delegate methods run but when I run watch os app it fails to execute any delegate method, and produce above error.

推荐答案

activate() 方法运行 异步 从 Watch OS v2.2 开始.因此,在您的代码中,在调用 activate() 后立即调用 updateApplicationContext 并不能保证在尝试更新应用程序上下文时实际激活会话.

The activate() method runs asynchronously from Watch OS v2.2 onwards. So, in your code, calling updateApplicationContext just after invoking activate() does not provide any guarantee that the session will be actually activated when trying to update the application context.

正确的流程是将您的消息移动到 session(_:activationDidCompleteWith:error:),例如:

The correct flow would be to move your message to session(_:activationDidCompleteWith:error:), as for example:

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    if activationState == .activated {
       // Update application context here
    }
}

这篇关于Watch OS 应用程序无法与 iOS 应用程序连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆