HandleWatchKitExtensionRequest not called

I am trying to start ios parent application from watchkit application. I am using url scheme to run the application. But it looks like

-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply 

never called. The watch app seems to launch the app in backgound. But the parent application does not process the watchkit request. I tried my approach in a new project and it works great. Is there something I need to pay attention to?

I already tried to debug> Attach to process> myapp and place a breakpoint inside the handleWatchKitExtensionRequest method to confirm whether it is called and not being called.

Here is the progress, I call openParentApplication when the button is pressed in the watch app.

 @IBAction func viewOniPhoneAction() { let userInfo: [NSObject : AnyObject] = [ "userID" : user.userID ] WKInterfaceController.openParentApplication(userInfo, reply: { (userInfo : [NSObject : AnyObject]!, error : NSError!) -> Void in }) } 

Here is my delegeate app

 - (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply { NSDictionary *replyDict = @{@"response": @"done"}; reply(replyDict); } 

I tried reply() in handleWatchKitExtensionRequest , but I got this error in the response block from the monitoring application

 Error Error Domain=com.apple.watchkit.errors Code=2 "The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]" UserInfo=0x60800026e0c0 {NSLocalizedDescription=The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]} 
+6
source share
3 answers

I got it to work !!! Having the same problem ...

Just increase the value of startBackgroundTaskWithExpirationHandler to a larger value if you still don't get the data !!! I used 2 seconds earlier, but my network is too weak.

I call openParentApplication when a button is clicked in the clock application:

 [WKInterfaceController openParentApplication:loadDetailChatDataDictionary reply:^(NSDictionary *replyInfo, NSError *error) { 

Here is my application delegate:

 - (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply { __block UIBackgroundTaskIdentifier bogusWorkaroundTask; bogusWorkaroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [[UIApplication sharedApplication] endBackgroundTask:bogusWorkaroundTask]; }]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // increase the time to a larger value if you still don't get the data!!! I used 2 secs previously but my network is too weak!!! [[UIApplication sharedApplication] endBackgroundTask:bogusWorkaroundTask]; }); // -------------------- __block UIBackgroundTaskIdentifier realBackgroundTask; realBackgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ reply(nil); [[UIApplication sharedApplication] endBackgroundTask:realBackgroundTask]; }]; NSString *value = userInfo[@"key"]; if ([value isEqualToString:@"loadRecentChatData"]) { reply(@{@"recents":recents}); // Add your reply here } 
+2
source

handleWatchKitRequest not called when opening an application through a URL scheme. It is called only in response to requests made in the WatchKit extension using openParentApplication:reply: That is why you do not see this being done.

0
source

You will need to wrap your answer in the background task so that your parent application has time to respond.

-

 (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *replyInfo))reply{ UIApplication *app = [UIApplication sharedApplication]; UIBackgroundTaskIdentifier bgTask __block = [app beginBackgroundTaskWithName:@"watchAppRequest" expirationHandler:^{ [[UIApplication sharedApplication] endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; //make your calls here to your tasks, when finished, send the reply then terminate the background task //send reply back to watch reply(replyInfo); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [app endBackgroundTask:bgTask]; bgTask=UIBackgroundTaskInvalid; }); } 
0
source

Source: https://habr.com/ru/post/987649/


All Articles