0

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.

  • Sounds like maybe a bug with in the OS. Probably best to file a bug report with Apple with a sample project attached. Great if you could report back here with the radar number! – ccjensen Jan 05 '16 at 10:44
  • Did you debug on the watch os side and are you sure that `session.transferUserInfo` is actually called? Are you sure that self.activityTitle is not nil? – Gerd Castan Jan 06 '16 at 21:32

2 Answers2

1

I have had similar problems and I think it might be something with the WCSession. Try re-creating the session variable in the completeButtonPressed before using it.

self.session = ... //initialize the session again
if self.session != nil {
Lachezar
  • 6,523
  • 3
  • 33
  • 34
0

https://forums.developer.apple.com/thread/43596

Currently there is an issue for watchOS 2.2 and iOS 9.3 where using method transferUserInfo to send data to protocol method

  • (void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary *)userInfo

will work in the beginning, then it would would cease to work.

When it stopped working, I did get it to work again after I restarted both my iPhone and Apple Watch.

Also, make sure to have your session as a member variable with strong property:

@interface InterfaceController() <WCSessionDelegate>
@property (strong, nonatomic) WCSession *session;
@end

if ([WCSession isSupported]) {
        self.session = [WCSession defaultSession];
        self.session.delegate = self;
        [self.session activateSession];
}

Another then to watch out for is to use main queue to update your UI:

- (void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *, id> *)userInfo {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"InterfaceController.m ------------ (watch received: %@)----------------", userInfo);
        [self.descriptionLabel setText:userInfo[@"emojiDescription"]];
    });
}
rtsao
  • 199
  • 1
  • 14