[英](Watch Connectivity) WCSession is missing its delegate (only in XCode11.0)
As a title, I found this error only in XCode11.0.作为标题,我只在 XCode11.0 中发现了这个错误。
I send some data from app (written with react-native) to appleWatch.我将一些数据从应用程序(用 react-native 编写)发送到 appleWatch。
I'm using now watch-connectivity and the method of 'updateApplicationContext' for communicating iPhone with it.我现在正在使用 watch-connectivity 和“updateApplicationContext”方法与 iPhone 进行通信。
But appleWatch returned the following error.但是 appleWatch 返回了以下错误。
The error log is below:错误日志如下:
(1) [WC] WCSession is missing its delegate
(2) [WC] -[WCSession handleApplicationContextWithPairingID:]
_block_invoke delegate (null) does not implement session:didReceiveApplicationContext:
The source code is below:源代码如下:
let session = WCSession.default
override func awake(withContext context: Any?) {
super.awake(withContext: context)
session.delegate = self
session.activate()
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
//
}
And I found error(1) is not happened in XCode10.3.而且我发现XCode10.3中没有发生错误(1)。
... error(2) is also happened. ...错误(2)也发生了。
Do you have any idea regarding this error(1) or (2)?您对此错误(1)或(2)有任何想法吗?
I guess this is caused by XCode11.0, but I couldn't locate what causes this.我猜这是由 XCode11.0 引起的,但我找不到是什么原因造成的。
Thanks.谢谢。
I fixed these errors.我修复了这些错误。
In fact, I implemented the following:事实上,我实现了以下内容:
override func awake(withContext context: Any?) {
super.awake(withContext: context)
if WCSession.isSupported() {
let session = WCSession.default
session.delegate = self
session.activate()
if session.isReachable {
print("your iphone is Reachable")
} else {
print("your iphone is not Reachable...")
}
} else {
print("This device is not supported.")
}
let aList = dataAccess.fetchData(id: nil)
let aControllers = [String](repeating: "aController", count: aList.count)
MainInterfaceController.reloadRootControllers(withNames: aControllers, contexts: aList)
}
I found that the last row causes this errors.我发现最后一行导致了这个错误。
I guess the method of 'reloadRootControllers' resets WCsession, even if it is already activated.我猜'reloadRootControllers'的方法会重置WCsession,即使它已经被激活。
So, I set session which is for receivedApplicationContext, but event is not happened and called as if 'delegate (null) does not implement session'.所以,我设置了 session 用于接收应用程序上下文,但事件没有发生并被调用,就好像“委托(null)没有实现会话”一样。
I am facing the same problem like you did I have no idea how to fix this.我面临着和你一样的问题,我不知道如何解决这个问题。
this is how my code looks like on the phone: (my use case is that the phone is the one receiving data from the watch):这是我的代码在手机上的样子:(我的用例是手机是从手表接收数据的那个):
import WatchConnectivity
class PhoneWorkingSet: NSObject, WCSessionDelegate {
override init() {
super.init()
}
func startSession() {
if WCSession.isSupported() {
let session = WCSession.default
session.delegate = self
session.activate()
}
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
print("Received: \(applicationContext)")
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if(!session.isReachable) {
print("Watch not reachable")
} else {
print("Watch Reachable")
}
}
func sessionDidBecomeInactive(_ session: WCSession) {
print("has contentPending: \(session.hasContentPending)")
}
func sessionDidDeactivate(_ session: WCSession) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.