I'm developing an Apple Watch application that uses the
openParentApplication:reply: method to communicate with its parent app.The parent app communicates with a web service and sends back the data it gets to the watch extension by means of calling the
reply method with a NSDictionary containing the data.The app works perfectly when the parent app is open in the foreground or background. But if I open the parent app and then terminate it using the task switcher, the first time the watch extension sends a request, it gets the following error and the parameter
replyInfo comes in as nil.UIApplicationDelegate in the iPhone App never called reply()The weird thing is, every single
openParentApplication:replyInfo: call the extension makes after that gets a proper response. It's as if the handleWatchKitExtensionRequest:reply: method in the parent is doesn't get called when the app is first launched the first time after it was killed.What could be the possible reason for this?
I'm performing all operations in the
handleWatchKitExtensionRequest:reply: in a background task, as suggested in the docs. Here's some of my code:Code from my extension:
NSDictionary *params = @{@"requestCode": @(SHBRequestGetLoggedIn)}; [WKInterfaceController openParentApplication:params reply:^(NSDictionary *replyInfo, NSError *error) {
// Do something with the result
}];Code from the parent app:- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
}];
NSNumber* requestCode = userInfo[@"requestCode"];
switch ([requestCode intValue])
{
// Perform some request and then call reply()
// End the background task
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
});
}
}