如何使用 WatchConnectivity 将数据发送回 iPhone
How can I send a data back to iPhone using WatchConnectivity
我有一个带有多个计数器的习惯应用程序。现在我弄清楚了如何将计数器的值发送到 iWatch。问题是:当我按下按钮并更改 iWatch 上的计数器值(例如 +1)时,我如何将数据发送回 iPhone?
要从 iPhone 发送计数值 我在 ViewController 中有一个代码:
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { }
func sessionDidBecomeInactive(_ session: WCSession) { }
func sessionDidDeactivate(_ session: WCSession) { }
var session: WCSession?
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if WCSession.isSupported() {
session = WCSession.default
session?.delegate = self
session?.activate()
}
}
@IBAction func addPlusOne(_ sender: UIButton) {
let count += 1
let sendHabit = String(count)
if let validSession = session {
let iPhoneAppCount = ["Habits": sendHabit]
do {
try validSession.updateApplicationContext(iPhoneAppCount)
} catch {
print("Something went wrong")
}
}
为了接收 iWatch 的计数值,我在 InterfaceController 中有代码:
let session = WCSession.default
override func awake(withContext context: Any?) {
super.awake(withContext: context)
processApplicationContext()
processApplicationCount()
session.delegate = self
session.activate()
}
func processApplicationCount() {
if let iPhoneContext = session.receivedApplicationContext as? [String : String] {
displayLabel.setText(iPhoneContext["Habits"])
}
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
DispatchQueue.main.async() {
self.processApplicationCount()
}
}
将数据从 Apple Watch 传输到 iPhone 与反之亦然。
对于全局变量,您可以使用 Watch 中 WCSession 的 updateApplicationContext():
let session = WCSession.default()
if session.activationState == .activated {
session.updateApplicationContext(["my_global": g_myGlobal])
}
然后在 phone 上,您应该将委托分配给 WCSession 并实现以下内容:
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
let receivedGlobal = applicationContext["my_global"] as? TypeOfTheGlobal
}
我有一个带有多个计数器的习惯应用程序。现在我弄清楚了如何将计数器的值发送到 iWatch。问题是:当我按下按钮并更改 iWatch 上的计数器值(例如 +1)时,我如何将数据发送回 iPhone?
要从 iPhone 发送计数值 我在 ViewController 中有一个代码:
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { }
func sessionDidBecomeInactive(_ session: WCSession) { }
func sessionDidDeactivate(_ session: WCSession) { }
var session: WCSession?
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if WCSession.isSupported() {
session = WCSession.default
session?.delegate = self
session?.activate()
}
}
@IBAction func addPlusOne(_ sender: UIButton) {
let count += 1
let sendHabit = String(count)
if let validSession = session {
let iPhoneAppCount = ["Habits": sendHabit]
do {
try validSession.updateApplicationContext(iPhoneAppCount)
} catch {
print("Something went wrong")
}
}
为了接收 iWatch 的计数值,我在 InterfaceController 中有代码:
let session = WCSession.default
override func awake(withContext context: Any?) {
super.awake(withContext: context)
processApplicationContext()
processApplicationCount()
session.delegate = self
session.activate()
}
func processApplicationCount() {
if let iPhoneContext = session.receivedApplicationContext as? [String : String] {
displayLabel.setText(iPhoneContext["Habits"])
}
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
DispatchQueue.main.async() {
self.processApplicationCount()
}
}
将数据从 Apple Watch 传输到 iPhone 与反之亦然。
对于全局变量,您可以使用 Watch 中 WCSession 的 updateApplicationContext():
let session = WCSession.default()
if session.activationState == .activated {
session.updateApplicationContext(["my_global": g_myGlobal])
}
然后在 phone 上,您应该将委托分配给 WCSession 并实现以下内容:
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
let receivedGlobal = applicationContext["my_global"] as? TypeOfTheGlobal
}