If your app is not active, openParentApplication cannot activate it. It can only perform tasks in the background. For this to work, it is important that you start a background task in handleWatchKitExtensionRequest
as specified in the documentation. This ensures that the main app on the iPhone is not suspended before it can send its reply.
Code in the app delegate of the main app on iPhone:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{
__block UIBackgroundTaskIdentifier watchKitHandler;
watchKitHandler = [[UIApplication sharedApplication]
beginBackgroundTaskWithName:@"backgroundTask"
expirationHandler:^{
watchKitHandler = UIBackgroundTaskInvalid;
}];
if ([[userInfo objectForKey:@"request"] isEqualToString:@"getData"]) {
// get data
// ...
reply( data );
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 1), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
});
}