[英]Watch OS app not able to connect with iOS app
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: 我正在尝试将我的Watch OS应用程序与iOS应用程序连接并获取一些数据,但是当我尝试连接iOS应用程序时出现以下错误:
[WC] __28-[WCSession activateSession]_block_invoke_2 sessionReadyForInitialStateWithCompletionHandler failed due to NSXPCConnectionInterrupted
由于NSXPCConnectionInterrupted,[WC] __28- [WCSession activateSession] _block_invoke_2 sessionReadyForInitialStateWithCompletionHandler失败
__44-[WCSession updateApplicationContext:error:]_block_invoke failed due to WCErrorCodeSessionNotActivated WatchConnectivity session has not been activated.
__44- [WCSession updateApplicationContext:error:] _ block_invoke由于WCErrorCodeSessionNotActivated而失败WatchConnectivity会话尚未激活。
iOS app Code: iOS应用代码:
- (void) startSession{
if ([WCSession isSupported]) {
self.session = [WCSession defaultSession];
self.session.delegate = self;
[self.session activateSession];
}
} }
Watch OS Code: 观看OS代码:
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 app is in objective C and watch app is in swift iOS应用程序是客观的C和观看应用程序是迅速的
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. 我首先执行我的iOS应用程序代码并运行委托方法,但是当我运行watch os app时,它无法执行任何委托方法,并产生上述错误。
The activate()
method runs asynchronously from Watch OS v2.2 onwards. activate()
方法从Watch OS v2.2开始异步运行。 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. 因此,在您的代码中,在调用
activate()
之后调用updateApplicationContext
并不能保证在尝试更新应用程序上下文时实际激活会话。
The correct flow would be to move your message to session(_:activationDidCompleteWith:error:)
, as for example: 正确的流程是将您的消息移动到
session(_:activationDidCompleteWith:error:)
,例如:
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if activationState == .activated {
// Update application context here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.