傳送資料
在觸發某些事件後,將資料從 iPhone 或從 iPhone 傳送到 Watch App 並非常簡單,儘管程式碼看起來有點複雜。
let message = ["key":"value-to-send"]
WCSession.default().sendMessage(message, replyHandler: { (replyMessage) in
print("Got a reply from the phone: \(replyMessage)")
if let returnedValues = replyMessage["returned-value"] as? NSArray {
for val in returnedValues {
// do something here with the data
// Dispatch to Main Thread if affecting UI
}
}
}, errorHandler: { (error) in
print("Got an error sending to the phone: \(error)")
})