Watch Connectivity第一次运行,但之后没有运行

时间:2016-11-25 00:30:52

标签: ios iphone swift watchkit

我已经在模拟器和设备上测试过了 Watch Connectivity在使用一次后停止工作

我从观看 - >传递数据iPhone,它只能运行一次然后在此之后停止。

有什么想法吗?

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)"
    }

}

观看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")
    }
}

2 个答案:

答案 0 :(得分:3)

使用updateApplicationContext()时,需要更改每个调用的参数,否则将无法传递消息。我相信这是为了节省电池。

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

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

答案 1 :(得分:2)

如果您想快速快速 排出Apple手表应用程序的电池电量,那么@Jens的上述技术就会很好。

而不是从 updateApplicationContext() 转移到 sendMessage() ,而不是在项目中进行小的更改得到期望的结果。

我将解释问题场景,然后解决方案以及Demo应用: -

enter image description here

 @IBAction func sliderChange(_ value: Float)

{
    let str:String?
    switch value {
    case 0...7 :
          str = "Running"
    case 8...14 :
          str = "Sleeping"
    default:
        str = "normal"
    }
     updateContext(value: str!)
}

func updateContext(value:String) {
    let dictionary = [ "watchType" : value ]
    do {
        print("update application context is called do statemet")
        try session?.updateApplicationContext(dictionary)
    }
    catch{
        print("this is the catch statement")
    }
}

随着Watch中滑块值的更新,iPhone值会更新。你可以看到iPhone的重复值,即

  

当sliderValue从0到7时,值仍为"正在运行" &安培;&安培; "睡眠"对于   8到14岁。

如果我改变幻灯片的值,应用程序正常工作,并且在正常情况下,所需的结果会反映在iPhone中。

  

失败的Scenrio : -   i)将滑块值从0更改为3然后"运行"反映在iPhone上。 Thant很好。   ii)现在关闭iPhone应用程序,然后将滑块值从3更改为5,当iPhone打开时,我们无法看到真正的问题。   iii)不会触发iPhone的值

enter image description here

  

由于updateApplicationContext()限制的内部缓存机制    触发重复值到iPhone。

将最后更新的状态存储在 didReceiveApplicationContext()中,并根据存储的值显示状态。

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        checkStatus()
        updateSavedState()
    }
    func checkStatus() {
        print("check status")
        if WCSession.isSupported() {
            session = WCSession.default()
            session?.delegate = self
            session?.activate()
        }
    }

     func session(_ session: WCSession, didReceiveApplicationContext    applicationContext: [String : Any]) {
        let type = applicationContext["watchType"]!
         DispatchQueue.main.async {
            self.updateLabel.text =  " Type: \(type)"
            UserDefaults.standard.set(type, forKey: "savedState") //setObject
        }
    }

    func updateSavedState() {
        self.updateLabel.text = UserDefaults.standard.string(forKey: "savedState")
    }

现在一切都很完美。 Demo App