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(@"");
}
];
}
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.
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).
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});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With