在模拟器上 运行 时,WatchConnectivity 在 iOS 端未激活

WatchConnectivity not activating on iOS side when running on Simulator

我正在尝试 运行 我在模拟器上的项目,但是 iOS 端没有激活,只有 Watch 端设法激活。

这是我的ScoresInterfaceController.swift(手表端)

import WatchConnectivity    

class ScoresInterfaceController: WKInterfaceController, WCSessionDelegate {

    // Used to send information to the iOS app
    var applicationDict = [String: Int]()

    // Starts a session to communicate with the iOS app
    var session: WCSession!

    // For WCSession
    override init() {
        super.init()

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

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

这是我的ScoreViewController.swift(iOS边)

import WatchConnectivity    

class ScoreViewController: UIViewController, WCSessionDelegate {    

    // Starts a session to communicate with the Watch app
    var session: WCSession!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        if(WCSession.isSupported()) {
            session = WCSession.default()
            session.delegate = self
            session.activate()    //Not activating when run on Simulator      
        }
    }

    // For WCSession

    /** Called when the session has completed activation. If session state is WCSessionActivationStateNotActivated there will be an error with more details. */
    @available(iOS 9.3, *)
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {}

    // Receives data from Watch app
    @nonobjc func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {}

    func sessionDidBecomeInactive(_ session: WCSession) {}

    func sessionDidDeactivate(_ session: WCSession) {
        WCSession.default().activate()
    }
}

错误信息如下:

我遵循了本教程,但我无法弄清楚问题所在:

http://kristina.io/watchos-2-how-to-communicate-between-devices-using-watch-connectivity/

从这里更改了我的 ScoreViewController 会话定义:

@nonobjc func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {}

对此:

func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any])