Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple Watch and openParentApplication in background

I am implementing a apple watch. In my extension of the Watchkit i use the method to communicate to the "main app".

    [WKInterfaceController openParentApplication:applicationData reply:^(NSDictionary *replyInfo, NSError *error) {}];

According to apple documentation, the app can handle the request in background.

When you call the openParentApplication:reply: method, iOS launches or 
wakes up the parent app in the background and calls the
application:handleWatchKitExtensionRequest:reply:method of its app delegate.

However my app always became active, even if i have no code inside the method, handleWatchKitExtensionRequest

Any tips if it´s possible?

Thanks in advance

like image 591
DaSilva Avatar asked Dec 17 '14 18:12

DaSilva


2 Answers

According to official response from Apple dev on Apple Developer Forum this is bug in Beta 2 of WatchKit Framework.

https://devforums.apple.com/message/1082689#1082689

And just to be clear about one thing, the iPhone app is lauched in the background. In the simulator currently the app is being launched in the foreground. This will not be the experience on device. The documentation specifically says, "Calling the method causes iOS to launch the app in the background ...".

(Post number 6)

BTW, yesterday Beta 3 was released, maybe it's already fixed.

What is more if iPhone will be locked your iOS app also will be launched in background.

like image 81
lvp Avatar answered Oct 12 '22 09:10

lvp


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];
   });
}
like image 30
vomako Avatar answered Oct 12 '22 09:10

vomako