我在 WatchOS 2.1 和 iOS 9 上使用 WatchConnectivity 框架时遇到问题。我正在使用 transferUserInfo() 将数据从 iOS 应用程序发送到 Watch 并且它可以工作。问题在回来的路上:当我在用户按下手表视图中的按钮后尝试发送更新时,通信只是偶尔有效。例如,它通常第一次工作,而其他所有时间似乎消息丢失了。我尝试了不同的解决方案(sendMessage、updateContext、ecc),但我总是遇到同样的问题。谁能帮我?这是代码:
//在手表端:
@IBAction func completeButtonPressed() {
if let index = isActivityTitled(activityTitle, inArray: todayActivities) {
activityCompleted = !activityCompleted
todayActivities[index].completed = activityCompleted
}
if self.activityCompleted == false {
self.completeButton.setBackgroundImage(UIImage(named: "completedButton"))
self.completeButton.setTitle("COMPLETA")
} else {
self.completeButton.setBackgroundImage(UIImage(named: "rectButtonGray"))
self.completeButton.setTitle("COMPLETATO")
}
if self.session != nil {
session.transferUserInfo(["WatchUpdatedActivityTitle":self.activityTitle])
}
}
//iOS端:(我在AppDelegate中处理回调)
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
print("Message From Watch:")
print(userInfo["WatchUpdatedActivityTitle"])
if let actTitle = userInfo["WatchUpdatedActivityTitle"] {
self.activitiesList = ActivitiesList.sharedActivitiesList
let updatedActivity = self.activitiesList.getActivityTitled(actTitle as! String)
if updatedActivity?.completed == false {
updatedActivity?.completed! = true
} else {
updatedActivity?.completed! = false
}
self.activitiesList.updateActivity(updatedActivity!, withReorder: true)
dispatch_async(dispatch_get_main_queue()) {
NSNotificationCenter.defaultCenter().postNotificationName("reloadTableViews", object: nil)
}
}
}
希望尽快解决问题,非常感谢。