Complication stops updating when Apple Watch is on the charger

Greetings all,
I have an interesting problem. When the user wears the watch, my app's complication updates approximately every 15 minutes. Although it is not a standalone app, it only uses the iPhone app to do a one time initialization. After that, all network access for data is done by my WatchOS app. Same with the complication. That's great.

But when my watch is on the charger my app stops updating. First the app is killed by WatchOS after an unknown amount of time. Then it is loaded in memory, but never updates the complication. When I tap on my complication the app starts and it's obvious that it is loaded in memory because it starts so fast.

If the watch gets an overnight WatchOS update and then the watch reboots itself, it is not even loaded into memory. Meanwhile Apple's Stock app is updated properly even after the watch rebooted and is still on the doc.

I've gone though and reviewed all of my code, ensured the any files that are accessed while the watch is locked don't have file protection enabled, and yet no luck.

Is background refresh really the way to do this or should I scrap that and use complication notifications instead?
Answered by sal_from_new_york in 636481022
After much testing with passcode set/not set on various watches, I've determined that if there is no passcode set on the watch, the app updates the complications when on the charger. If the passcode is set then I get the "operation not permitted" error. Perhaps the temp files are created with complete file protection and as such are not allowed when the watch is locked.

So the issue was not having the watch on the charger but rather that the device was locked. Furthermore, my app is no longer dead when I take the watch off the charger in the morning.
Now when I read this in another context :) ... this came into my mind:

Code Block
   func getPrivacyBehavior(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void) {
      // Call the handler with your desired behavior when the device is locked
      handler(.showOnLockScreen)
   }


This maybe something you do already and my comment is not relevant. Surely you can see that the complication is not active when you take the watch from your wrist, if the above code is not in the CLKComplicationDataSource.

Hi Andy,
Thanks for the suggestion, unfortunately I already have that. The one thing my app is doing is that when it transitions to the background it explicitly suspends timers and if started by WatchOS in the background my app does not fully initialize itself by design. I thought that was okay, especially since the complication update code is self contained and doesn't really need the app portion to initialize itself until it's in the foreground. I thought I was supposed to do that, but maybe I shouldn't?
It appears that when the app resumes the URLSession's download task, less than 50 milliseconds later, the request fails:
Code Block
Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" UserInfo={NSErrorFailingURLStringKey=https:[my request url],
NSErrorFailingURLKey=https:[my request url],
_NSURLErrorRelatedURLSessionTaskErrorKey=(
"BackgroundDownloadTask <UUID1>.<1>",
"LocalDownloadTask <UUID2>.<1>"
), _NSURLErrorFailingURLSessiontaskErrorKey=BackgroundDownloadTask <UUID3>.<1>}


How do I determine the reason why the operation is not permitted when the app is launched in the background? This seems to be the reason why my complication can't update when started by WatchOS.

Anyone care to take a guess?
Accepted Answer
After much testing with passcode set/not set on various watches, I've determined that if there is no passcode set on the watch, the app updates the complications when on the charger. If the passcode is set then I get the "operation not permitted" error. Perhaps the temp files are created with complete file protection and as such are not allowed when the watch is locked.

So the issue was not having the watch on the charger but rather that the device was locked. Furthermore, my app is no longer dead when I take the watch off the charger in the morning.
Complication stops updating when Apple Watch is on the charger
 
 
Q