如何通过 Watch Connectivity 将多个“应用程序更新”从手机发送到手表(例如数组中的多个不同值)?
我的应用程序更新成功地通过numberItem
表格视图中选定单元格的值发送,但我还想通过选定单元格数组中的用户 ID 值发送。
现在,它只识别一个值,不会更新另一个值,而是将“请重试”显示为我的标签文本。
对于其他附加值(例如用户 ID 和用户名),我如何发送两个或多个应用程序更新。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let numberItem = number[indexPath.row]
print("tableview select #:")
print(numberItem)
do {
try WatchSessionManager.sharedManager.updateApplicationContext(["number" : numberItem])
} catch {
let alertController = UIAlertController(title: "Oops!", message: "Looks like your \(numberItem) got stuck on the way! Please send again!", preferredStyle: .Alert)
presentViewController(alertController, animated: true, completion: nil)
}
let uidItem = 15
//let uidDict = ["uidValue":uidItem]
print("the send UID is")
//print(uidItem)
do {
try WatchSessionManager.sharedManager.updateApplicationContext(["uidValue" : uidItem])
} catch {
let alertController = UIAlertController(title: "Oops!", message: "Looks like your \(uidItem) got stuck on the way! Please send again!", preferredStyle: .Alert)
presentViewController(alertController, animated: true, completion: nil)
}
}
我的 datasource.swift 文件是:
enum Item {
case Number(String)
case uidValue(String)
case Unknown
}
init(data: [String : AnyObject]) {
if let numberItem = data["number"] as? String {
item = Item.Number(numberItem)
print("enum item is")
print(numberItem)
} else if let uidItem = data["uidValue"] as? String {
item = Item.uidValue(uidItem)
print("enum item is")
print(uidItem)
} else {
item = Item.Unknown
}
}
我在手表上的接口控制器(连接到我的数据标签)包括:
func dataSourceDidUpdate(dataSource: DataSource) {
switch dataSource.item {
// the first application update- commented out to try the 2nd update
//case .Number(let numberItem):
// titleLabel.setText(numberItem)
// print(numberItem)
// the second application update
case .uidValue(let uidItem):
uidLabel.setText(uidItem)
print(uidItem)
case .Unknown:
nidLabel.setText("please retry")
default:
print("default")
}
}