[英]Watch Connectivity works the first time but not after that
我已经在Simulator和Device上进行了测试,一旦使用Watch Watch Connectivity ,它就会以某种方式停止工作 。
我从Watch-> 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")
}
}
当使用updateApplicationContext()
,您需要更改每个调用的参数,否则将不发送msg。 我相信这是为了节省电池。
无论如何,尝试使用sendMessage()
或sendMessageData()
发送消息,即使消息内容相同,每次也会传递消息。 而且,它们的优先级高于updateApplicationContext
因此是双赢的:)
这是文档: https : //developer.apple.com/library/content/documentation/General/Conceptual/WatchKitProgrammingGuide/SharingData.html#//apple_ref/doc/uid/TP40014969-CH29-SW1
如果您想快速耗尽Apple Watch App的电池,那么@Jens的上述技巧很好。
与其从updateApplicationContext()更改为sendMessage(),不如在项目中进行一些小的更改以获得所需的结果。
我将解释问题场景 ,然后与演示应用程序一起解决:-
@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滑块中的Values更新,iPhone值会更新。如您所见,iPhone的值存在重复,即
当SliderValue从0到7时,“ Running”和“&Sleeping”值保持8到14。
如果我更改幻灯片的值,并且正常情况下在iPhone中反映了所需的结果,则应用程序工作正常。
失败的情况 :-i)将滑块值从0更改为3,然后“ Running”会反映在iPhone中。 Thant很好。 ii)现在关闭iPhone应用程序,然后将滑块值从3更改为5,不,打开iPhone后我们看不到真正的问题。 iii) iPhone不会触发值 。
由于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")
}
现在一切都完美无缺。 演示应用程序 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.