Trying to upgrade my Watch OS1 app to Watch OS 2. Created new target for Watch OS 2. And using sendMessage:replyHandler:errorHandler
to send/ get replies from IOS app. It working fine if only if IOS app is running. If Watch app try communicate when iOS app is in inactive state(Killed State), getting connectivity error 7001. How to communicate inactive IOS app from Watch App( Watch OS 2)?
Will this sendMessage:replyHandler:errorHandler
method from watch app wakes up the corresponding iOS app in the background and makes it reachable?
Thanks.
Edit1:-
iOS APP's App Delegate:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
return YES;
}
- (void)session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary<NSString *,id> *)message replyHandler:(nonnull void (^)(NSDictionary<NSString *,id> * __nonnull))replyHandler {
UIApplication *application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier identifier = UIBackgroundTaskInvalid;
dispatch_block_t endBlock = ^ {
if (identifier != UIBackgroundTaskInvalid) {
[application endBackgroundTask:identifier];
}
identifier = UIBackgroundTaskInvalid;
};
identifier = [application beginBackgroundTaskWithExpirationHandler:endBlock];
if (replyHandler!=nil) {
replyHandler(resultContainer); // my data dictionary from Iphone app to watch os as reply.
}
if (identifier!=UIBackgroundTaskInvalid) {
[application endBackgroundTask:identifier];
identifier = UIBackgroundTaskInvalid;
}
}
Watch App:
- (void)applicationDidFinishLaunching {
// Perform any final initialization of your application.
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
NSDictionary *context = @{@"APP_LOADING":@"LOADING"};
[WKInterfaceController reloadRootControllersWithNames:@[WATCH_INTERFACE_LOADING] contexts:@[context]];
NSDictionary *request = //My Request data;
[[WCSession defaultSession] sendMessage:request
replyHandler:^(NSDictionary *reply) {
//handle reply from iPhone app here
NSDictionary *resultDict = [reply objectForKey:WATCH_REQUEST_RESULT];
// Use reply from Phone app
}
errorHandler:^(NSError *error) {
//catch any errors here
// Getting error here 7001 Error.
}
];
}