1

The code below helps to get the data sent from iWatch to iPhone .But not from iPhone to iWatch if we write the code -(void)send:(NSString *)action and in Appdelegate

 (void)session:(nonnull WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message replyHandler:(void(^)(NSDictionary<NSString *,id> *))replyHandler in wacth extension

In the watch extension controller

 -(void)send:(NSString *)action 
  {
    NSDictionary *request = @{@"request":action};
    [[WCSession defaultSession] sendMessage:request
      replyHandler:^(NSDictionary *replyHandler) {
     [self setTextForLabelWithData:[replyHandler valueForKey:@"response"]];
         }
     errorHandler:^(NSError *error) {
          NSLog(@"");
    }];
}

in Appdelegate

- (void)session:(nonnull WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message replyHandler:(void(^)(NSDictionary<NSString *,id> *))replyHandler
  { 
    NSString *action = message[@"request"];
    NSString *actionPerformed;
    if ([action isEqual:@"foo"]) {
        // do "foo" stuff
        actionPerformed = @"foo done";
    } else if ([action isEqual:@"bar"]) {
        // do "bar" stuff
        actionPerformed = @"bar done";
    }
    replyHandler(@{@"actionPerformed":actionPerformed});
}

How can it be done ? Please help.

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
Piya
  • 295
  • 1
  • 2
  • 14

2 Answers2

0

I guess you need to update the key from "response" at this line

 [self setTextForLabelWithData:[replyHandler valueForKey:@"response"]];

to "actionPerformed" to be like that:

 [self setTextForLabelWithData:[replyHandler valueForKey:@"actionPerformed"]];
0

You need to implement Watch Connectivity delegate method request to iPhone to Apple Watch and Apple Watch to iPhone.

For more information see tutorial : http://www.codingexplorer.com/watch-connectivity-swift-application-context/

Shahabuddin Vansiwala
  • 673
  • 1
  • 13
  • 24