Watch Connectivity 第一次工作,但之后就不行了

SRM*_*RMR 3 iphone ios swift watchkit

我已经在模拟器和设备上进行了测试,不知何故手表连接停止工作在使用一次后。

我正在从Watch -> iPhone传递数据,它只能工作一次,然后停止。

有任何想法吗?

苹果手机ViewController

var session: WCSession?

override func viewDidLoad() {
    super.viewDidLoad()
    if WCSession.isSupported() {
        session = WCSession.default()
        session?.delegate = self
        session?.activate()
    }
}

func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
    // Received Application Context from Watch
    let type = applicationContext["watchType"]
    print("Type iPhone: \(type)")

    DispatchQueue.main.async {
        self.type.text = "Type: \(type)"
    }

}
Run Code Online (Sandbox Code Playgroud)

观看InterfaceController

let session = WCSession.default()

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

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

@IBAction func startPressed() {
    saveEverything()
}

func saveEverything() {
    let watchContextType = ["watchType" : "Boxing/Running"]

    do {
        print("Type Watch: \(watchContextType)")
        try session.updateApplicationContext(watchContextType)
    } catch {
        print("Didn't work")
    }
}
Run Code Online (Sandbox Code Playgroud)

Jen*_*ter 5

使用的时候updateApplicationContext(),每次调用都需要修改参数,否则msg不会投递。我相信这是为了节省电池。

无论如何,尝试使用sendMessage()或发送您的消息sendMessageData(),然后每次都会发送消息,即使它们具有相同的内容。而且,它们的优先级高于updateApplicationContext所以这是双赢的:)

这是文档:https : //developer.apple.com/library/content/documentation/General/Conceptual/WatchKitProgrammingGuide/SharingData.html#//apple_ref/doc/uid/TP40014969-CH29-SW1