8

I am running WatchOS 2.0 on Version 7.0 beta 5. I am running an iOS with iWatch App.

I setup the Targets as shown.

enter image description here

I had my iOS's ViewController and WatchKitExtension's Interface Controller both activated WCSession and set as delegate.

if ([WCSession isSupported]) {
    WCSession *session = [WCSession defaultSession];
    session.delegate = self;
    [session activateSession];
    NSLog(@"iOS App WCSession is supported");
}

Then I tried to send userInfo from Watch to iOS :

NSDictionary *userInfo = [[NSDictionary alloc]initWithObjectsAndKeys:@"testingURL", @"outputURL", nil];
        [[WCSession defaultSession] transferUserInfo:userInfo];

But my ViewController's delegate method never get called:

- (void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *,id> *)userInfo{

dispatch_async(dispatch_get_main_queue(), ^{
    NSLog(@"Received userInfo Transferr");
    NSLog(@"%@", userInfo);
    [self.label setText:@"Received"];
});
}

I was running the Watch App and iOS together from Simulator by pressing Run here from this scheme:

enter image description here

Could anyone please tell me what did I do wrong?

John
  • 2,672
  • 2
  • 23
  • 29
  • I can't see anything obvious wrong. I'd suggest filing a bug with Apple (include your test project) and reporting back here with the radar number so that any perusing Apple engineers can quickly track it down! – ccjensen Aug 23 '15 at 14:11

2 Answers2

5

In general, it is not a good idea to receive WCSession data in a UIViewcontroller, since you never can be sure whether it is there or not.

Apple says you should start receiving as soon as possible. Your UIApplicationDelegate is a good place to receive data from WCSession and a good place to set it up as early as possible.

Edit

You also don't hold a reference to your activated session on the watch side. This means Apple can remove all the session ressources.

So you next call to defaultSession gets you a fresh unactivated session.

Edit 2

In my experience you have to do 2 things when testing the communication between WatchApp Extension and iOS App:

  1. Start WatchApp from XCode 7 (sometimes I have to do this twice)
  2. go into the iOS Simulator and start your iOS App manually

There may be more ways to make sure both run and can communicate.

Also try to send a message from iOS App to your WatchApp extension, this works for me.

Gerd Castan
  • 6,275
  • 3
  • 44
  • 89
  • Thanks Gerd for your suggestion. I moved the WCSessionDelegate to AppDelegate but still didReceiveUserInfo in AppDelegate not getting called. Any other ideas? – John Aug 23 '15 at 08:37
  • On the watch side, you get (another?) defaultSession again before sending. You don't use the remembered activated session instance. This may or may not be a problem. Apple may or may not do things when you call defaultSession again. – Gerd Castan Aug 23 '15 at 13:24
  • When you don't hold a reference to your session, the runtime system will probably remove all resources. So your session is not activated, it is a new one. – Gerd Castan Aug 23 '15 at 13:27
  • 1
    A singleton means that the session object doesn't exist twice. But a singleton does not guarantee you, that the object exists at all. If you don't hold a reference, Apple is free to throw everything away. – Gerd Castan Aug 23 '15 at 15:29
  • I gave another try to save @property WCSession of the defaultSession on both iOS and InterfaceController in Extension, still no delegate called... – John Aug 23 '15 at 19:13
  • The only hint that I have left: In the simulator, i start the watch from xcode. After that, I have to start the iOS App in the iPhone simulator . This is the last hint, that I have :-) – Gerd Castan Aug 23 '15 at 19:24
  • @GerdCastan, thanks again for your effort. So you get the WCSession delegates working on both sides in Simulator? Did you run the iOS and Watch app on scheme like mine? – John Aug 23 '15 at 19:50
  • At the monent I send from ios to watch os. I halucinate that if they can talk in one direction, the other one should be no problem – Gerd Castan Aug 23 '15 at 19:53
  • 1
    I don't understand why, but everything suddenly start to work after I send my first userInfo from iOS App to WatchKit, rather than the other round. I guess, maybe, at least in Simulator, the first userInfo must be sent from iOS App to WatchKit, then the WatchKit can start sending userInfo to iOS App anytime later on. Thanks Gred for helping me out and hinting me to try to send from App to Watch that started to make things work. – John Aug 24 '15 at 11:12
  • maybe you can edit your answer to suggest trying sending userInfo from iOS to WatchKit, and let me accept that to be an answer. At least that was the solution for my case. – John Aug 24 '15 at 11:14
  • I added the last 2 Hints, including sending to the WatchApp Extension – Gerd Castan Aug 24 '15 at 12:20
  • Just to add to this again, its very important to close all your iphone and iwatch simulators and then just launch iwatch simulator only. This is the only sequence of events that will work. FYI to anyone in future – Sam B Nov 10 '15 at 21:13
0

For me, when I stop receiving send events on either the watch or the iOS App, I just close both simulators and let XCode start them up again. They just seem to get into a state. I've never had this happen on an actual watch or iOS device.

cwgso
  • 401
  • 3
  • 9