Search code examples
iosios9today-extension

How to tell if iOS Today Widget is being updated in the background?


I have an iOS Today Widget that makes an expensive API call (expensive as in the more it is called, the more I pay money). If I implement widgetPerformUpdateWithCompletionHandler in my widget, then the widget can be updated in the background several times a day without the user ever looking at their Notification Center.

In an app, there is a key-value-pair in the options dictionary of application:didFinishLaunchingWithOptions that tells you if the app is launching the background or not, but there isn't anything similar in a widget that I can find. There is also the call to get the applicationState in UIApplication, which also isn't available in a widget.

So, is there any reliable way in my widget to know that the system is updating my widget in the background and the user isn't looking at their Notification Center to save myself from unnecessary API calls? I'm fine with the content not being completely up to date when the user does look at the widget because I can update it pretty quickly after it loads.

I've been trying to find a hacky solution like something different with the view or window that is different when the widget is being updated in the background, but haven't found anything yet. I've noticed the occasionally when the widget is initialized in the background that the window frame has a height of zero, but that doesn't seem to always be the case.

I'm also aware that if I don't implement widgetPerformUpdateWithCompletionHandler, then my widget isn't updated in the background which is a possible solution, but I'm looking for a way that I can have my cake and eat it too.

If anyone has any other ideas, I'm all ears. Thanks.


Solution

  • I filed an Apple Developer Technical Support ticket on this. They responded and told me that this was not possible that that I would need to file an enhancement request for this. I filed rdar://27434852.