Apple Watch and openParentApplication in the background

I use an apple watch. In my Watchkit extension, I use the method to communicate with the "main application".

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

According to apple documentation, an application can process a request in the 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 application always became active, even if I don't have code inside the method, handleWatchKitExtensionRequest

Any tips if possible?

Thanks in advance

+5
source share
4 answers

According to Apple Dev's official answer on the Apple Developers Forum, this is a bug in WatchKit Framework beta 2.

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

And just to be clear about one thing, the iPhone app is welcome in the background. Currently, in the simulator, the application runs in the foreground. This will not be an experience with the device. in the documentation, in particular, it says: "Calling a method causes iOS to run the application in the background ...".

(Postal number 6)

BTW, beta 3 was released yesterday, it may already be fixed.

Moreover, if the iPhone is locked, your iOS application will also be running in the background.

+2
source

If your application is inactive, openParentApplication cannot activate it. It can only perform tasks in the background. To do this, it is important to start the background job in handleWatchKitExtensionRequest , as indicated in the documentation . This ensures that the main application on the iPhone is not paused until a response is sent.

Code in the application application of the main application on the 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]; }); } 
+2
source

I think that in the "Features" section of the main application there is no background mode and it will be added in the future.

I tried to turn on all currently supported background modes, and none of them worked (i.e. the main application always started).

My review is patience until the next beta.

+1
source

I had the same issue with WatchKit in an existing application. I had a UIApplicationExitsOnSuspend (aka Application does not work in the background) installed in YES in info.plist. Change it to NO and the error will disappear.

0
source

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


All Articles