我在调用session.activateSession()方法时遇到WatchKit连接会话无法激活的问题。这是我用来设置会话的代码。
if (WCSession.isSupported()) {
session = WCSession.defaultSession()
session.delegate = self // conforms to WCSessionDelegate
session.activateSession()
print("Session has been activated")
}
但是,我在打印行上放置了一个断点,当我检查session对象时,它显示sessionActivated属性仍然为false,即使在调用activateSession之后也是如此。当我调用activate session时,我似乎没有收到任何类型的bug,所以我认为它应该可以工作,但事实似乎并非如此。
此外,如果我稍后在代码中尝试在session对象上使用sendMessage方法,如下所示-
let message = ["request": "fireLocalNotification"]
session.sendMessage(
message, replyHandler: { (replyMessage) -> Void in }) { (error) -> Void in
print(error.localizedDescription)
}
我收到一个错误代码“无法完成操作。(WCErrorDomain错误7004。)”我查了一下,意思是“WCErrorCodeSessionNotActivated”这是我认为activateSession方法没有正确调用的另一个原因。我甚至尝试在发送消息之前直接在行中运行activateSession方法,但仍然收到错误。如果有人能帮我解释一下发生了什么,那就太好了,谢谢!:)
发布于 2015-11-10 20:46:11
您应该在WatchKit扩展和iOS应用程序目标上激活WatchConnectivity会话。例如,您可以在InterfaceController的
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
if WCSession.isSupported() {
let wcsession = WCSession.defaultSession()
wcsession.delegate = self
wcsession.activateSession()
wcsession.sendMessage(["update": "list"], replyHandler: { (dict) -> Void in
print("InterfaceController session response: \(dict)")
}, errorHandler: { (error) -> Void in
print("InterfaceController session error: \(error)")
})
}
}
在AppDelegate中
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if WCSession.isSupported() {
let wcsession = WCSession.defaultSession()
wcsession.delegate = self
wcsession.activateSession()
}
return true
}
我在几个例子中注意到,人们倾向于只在处理请求的类中设置委托,例如,如果手表要向iOS应用程序发送消息,则只会在iOS应用程序中设置委托。这是错误的。正如WatchConnectivity明确声明的那样,您必须在这两种情况下都设置委托,否则将得到7004错误。
发布于 2017-07-31 04:15:19
由于在Xcode8中将"activateSession()“改为"activate()”,因此您需要为您的类添加和扩展以委托会话( WCSessionDelegate ),并使用以下函数对其进行扩展:
会话功能会话(_
:WCSession,activationDidCompleteWith activationState: WCSessionActivationState,error: Error?)
以确保异步方法"activate“成功完成。
在您的案例中:
extension YourInterfaceControllerClass : WCSessionDelegate {
func session(_ session: WCSession,
didReceiveMessage message: [String : Any],
replyHandler: @escaping ([String : Any]) -> Void)
{
//this function is mandatory and can be empty
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?)
{
// Your code to be executed after asynchronous activation is completed:
let message = ["request": "fireLocalNotification"]
session.sendMessage(
message, replyHandler: { (replyMessage) -> Void in }) { (error) -> Void in
print(error.localizedDescription)
}
//....
}
}
发布于 2015-08-01 11:08:38
你在使用大数值吗?
NSDictionary *userInfo = @{
@"a1":@(1000000000), // 1000000000
@"a2":@(10000000000), // 1e+10
@"a3":@(100000000000), // crash!
};
[[WCSession defaultSession] transferUserInfo:userInfo];
在上面的代码中,键"a3“的值是危险的,它会导致Apple Watch崩溃。
一旦你发送了列表,它就会一直保留在Apple Watch中,直到重新安装手表应用程序。
(这种崩溃发生在设备上,而不是模拟器上)
https://stackoverflow.com/questions/31191296
复制相似问题