WCSession无法激活 [英] WCSession Failing to Activate

查看:269
本文介绍了WCSession无法激活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用session.activateSession()方法时,我遇到了WatchKit连接会话无法激活的问题.这是我用来建立会话的代码.

I am having a problem with the WatchKit Connectivity Session failing to activate when I call the session.activateSession() method. This is the code I am using to set up the session.

if (WCSession.isSupported()) {
    session = WCSession.defaultSession()
    session.delegate = self // conforms to WCSessionDelegate
    session.activateSession()
    print("Session has been activated")
}

但是,我在打印行上放置了一个断点,当我检查会话对象时,它说,即使在调用activateSession之后,sessionActivated属性仍然为false.当我调用激活会话时,我似乎没有得到任何错误,因此我认为它应该可以工作,但事实并非如此.

However, I have placed a breakpoint on the print line and when I inspect the session object, it says the sessionActivated property is still false, even after calling activateSession. I don't appear to be getting any sort of bug when I call activate session, so I assume it should have worked, but this does not seem to be the case.

此外,如果稍后我在这样的代码中尝试在会话对象上使用sendMessage方法-

Furthermore, if I try and use the sendMessage method on the session object later in my code like this -

let message = ["request": "fireLocalNotification"]
session.sendMessage(
    message, replyHandler: { (replyMessage) -> Void in }) { (error) -> Void in
        print(error.localizedDescription)
    }

我收到一个错误代码该操作无法完成.(WCErrorDomain错误7004.)",它表示"WCErrorCodeSessionNotActivated".这是我认为activateSession方法未正确调用的另一个原因.在发送消息之前,我什至尝试直接在该行上运行ActivateSession方法,但仍然收到错误.如果有人可以帮助解释发生了什么,那就太好了,谢谢! :)

I receive an error code "The operation couldn’t be completed. (WCErrorDomain error 7004.)" which I looked up which means "WCErrorCodeSessionNotActivated." This is yet another reason why I think the activateSession method isn't calling correctly. I have even tried running the activateSession method the line directly before I send the message, but I still receive the error. If anyone could help explain what is going on, that would be wonderful, thank you! :)

推荐答案

您应同时在WatchKit Extension和iOS应用程序目标上激活WatchConnectivity会话.例如,您可以在InterfaceController的

You should activate the WatchConnectivity session on both the WatchKit Extension and the iOS app target. For example you might do it in the InterfaceController's

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中

and in the 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错误.

What I have noticed in several examples is that people tend to set a delegate only in the class which handles requests, e.g. if the watch was to send a message to the iOS app a delegate would only be set in the iOS app. This is WRONG. As the WatchConnectivity clearly states, you MUST set the delegate in both circumstances, otherwise you'll get the 7004 error.

这篇关于WCSession无法激活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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