WCSession尚未激活

时间:2016-11-20 18:48:39

标签: swift

当我尝试发送内容时,我得到WCSession尚未激活错误。而且我不知道自己做错了什么。我已经测试了serval预制解决方案" must"工作。但它不能在我的模拟器和物理设备上工作。

一些代码:

我的应用代表:

import UIKit
import CoreData
import WatchConnectivity


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {

    var window: UIWindow?


    func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : String]) -> Void) {
        replyHandler(["message": "Hello Watch!"])
    }

    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    }

    func sessionDidBecomeInactive(_ session: WCSession) {

    }

    func sessionDidDeactivate(_ session: WCSession) {

    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        if WCSession.isSupported() {
            let session = WCSession.default()
            session.delegate = self
            session.activate()
        }

        return true
    }

........


}

我的interfaceController:

import WatchKit
import Foundation
import WatchConnectivity


class InterfaceController: WKInterfaceController {


    override func awake(withContext context: Any?) {
        super.awake(withContext: context)

        // Configure interface objects here.
    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()

        if WCSession.default().isReachable {
            let messageDict = ["message": "hello iPhone!"]

            WCSession.default().sendMessage(messageDict, replyHandler: { (replyDict) -> Void in
                //print(replyDict)
            }, errorHandler: { (error) -> Void in
               // print(error)
            })
        }

    }

    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    }

    func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {

    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }
}

extensionDelegate:

   import WatchKit
    import WatchConnectivity

        class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {

        func applicationDidFinishLaunching() {
            // Perform any final initialization of your application.
            if WCSession.isSupported() {
                let session = WCSession.default()
                session.delegate = self
                session.activate()
            }
        }

        func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        }

        func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {

            }
    ....

是否有任何错误的代码或实现?因为没有什么对我有用。

2 个答案:

答案 0 :(得分:17)

我发现了我遇到的问题,我觉得你的问题很相似。我在调用activate()之后立即尝试发送消息,这在过去有效,但自从引入session(activationDidCompleteWith:)函数后,我需要从那里发送消息。

所以我认为您的解决方案就是其中之一:

  1. 让您的InterfaceController成为WCSessionDelegate,全部移动 WCSession激活那里,并发送消息 session(activationDidCompleteWith:)
  2. 发送消息 session(activationDidCompleteWith:)中的ExtensionDelegate(在我的身上) app我处理ExtensionDelegate
  3. 中的所有通信
  4. session(activationDidCompleteWith:)发布通知 ExtensionDelegate并添加一个观察者来处理它 InterfaceController
  5. 除此之外,我认为您可能也有问题,因为您的AppDelegate中session(didReceiveMessage:)的声明是错误的(或者至少我在尝试您的代码时遇到了问题。)

    新声明是:

    func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void)
    

答案 1 :(得分:0)

要检查您的代码是否看起来不错并且WCsession仍然无法激活,请重新启动手表。这就是我的答案。