I'm having troubles using the WatchConnectivity framework on WatchOS 2.1 and iOS 9. I'm using transferUserInfo() to send datas from the iOS app to the Watch and It works. The problem is on the way back: when I try to send an update after the user press a button in a view on the Watch, the communication works only occasionally. For example It usually works the first time and all the other times it seems like the message is lost. I've tried different solutions (sendMessage, updateContext, ecc) but I'm always experiencing the same issue. Can anyone help me? Here's the code:
//On the Watch side:
@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])
}
}
//On the iOS side: (I handle the callback in the 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)
}
}
}
Hoping to solve the issue soon, Thank you very much.