Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share data between Watch app and iOS app in watchOS2

I send a dictionary to an iOS app using:

- (void)sendMessage:(NSDictionary<NSString *, id> *)message 
  replyHandler:(nullable void (^)(NSDictionary<NSString *, id> *replyMessage))replyHandler 
  errorHandler:(nullable void (^)(NSError *error))errorHandler;

but I'm getting an error with this. I want to know how our parent app handle this request in watchOS2.

In watchOS1, I used openParentApplication to get data from the parent app, and Appdelegate had a handleWatchKitExtensionRequest to handle that request. How do we handle this in watchOS2?

My Interface Controller:

- (void)awakeWithContext:(id)context 
{
   [super awakeWithContext:context];
   if([WCSession isSupported])
   {
     self.watchSession = [WCSession defaultSession];
     self.watchSession.delegate = self;
     [self.watchSession activateSession];
   }
   [self sendRequestWithActionType:@"InitialView"];
}

-(void)sendRequestWithActionType:(NSString *)action
{
    NSDictionary *requst = @{@"request":action};
    [[WCSession defaultSession] sendMessage:requst
                           replyHandler:^(NSDictionary *replyHandler) {
                               [self setTextForLabelWithData:[replyHandler valueForKey:@"response"]];
                           }
                           errorHandler:^(NSError *error) {
                               NSLog(@"");
                           }
 ];
}
like image 649
Pramuka Dias Avatar asked Sep 24 '15 08:09

Pramuka Dias


People also ask

Can you share data between Apple Watch and iPhone?

Any data usage would be via the phone or via whatever wifi connection you might have. If you get a GPS only Apple Watch, then when you are away from your home WiFi, the Apple Watch will be using the iPhone's radios, including its cellular data. Bottom line, the Apple Watch is going to use your iPhone's data.

How do I share data between iOS apps?

User Defaults Group You have to enable app groups in both your app's and extension's targets as well as at the developer's portal. Then you are able to use UserDefaults container which can be shared between several apps (or between the app and the extension).


1 Answers

Here is an example of using sendMessage with a reply:

Sending side:

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

Receiving side:

- (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});
}
like image 156
ccjensen Avatar answered Sep 24 '22 09:09

ccjensen