How can an iOS app run a function in the background in a recurring manner (i.e. once an hour)? - iphone

On devices which support multitasking, I'd like my iOS app to run a function in the 'background' when the app is not running (i.e. it's suspended). I know that iOS supports running tasks in the background, but I'm not sure how to make the function recurring (and only when the app is not in the background). What's the best approach?
I'm not interested in running a long-term function in the background but a short-term function to simply update the application badge #. However this number is dynamically based on the app's data, and needs to run a query against core data.
To further clarify, yes, my core data will not be changing, but the badge represents a number of items due. As time progresses, more items will be due, so I want to update the badge to show the proper items due as time progresses. So if 5 items are due now, but half an hour later 3 more items are due, then by the time the next hour comes around, 8 items will be due even though the core data has not changed at all in and of itself.

It can't, the only task allowed to run in background are: audio, voip and location.

Why do you need to update the badge data every hour if the data stored within Core Data is not changing? ie. the app isn't running?
You can do this using push notifications, like previously posted, or you can use a scheduled local notification based on the data when the app is closed or backgrounded. I think those are pretty well your only options.

Related

Apple watch complications update limit

I have a complication that may need to update every 5 minutes. this could easily sum up to 120 updates per day. Is there a way to only update when the user wake up the watch?
I belief the answer to your question in NO, there is currently no way to only update the complication when the user wakes up the watch.
The reasons:
The idea of a complication is that the user sees it as soon as he or she raises the watch and activates the display.
There is simply no time to activate or launch an app, nor to download a real-time value to display it as a complication.
Thus, the data must be present before the user watches the watch face with the complication.
In many cases, this can be ensured, if the data don’t change too often:
On watchOS, it is possible to schedule background tasks at certain intervals that download actual data from a server. In your case, you would like to schedule regularly (in your case about every 5 min) a WKApplicationRefreshBackgroundTask to download the actual value, but due to power consumption reasons, the number of tasks is limited:
Background app refresh tasks are budgeted. In general, the system
performs approximately one task per hour for each app in the dock
(including the most recently used app). This budget is shared among
all apps on the dock. The system performs multiple tasks an hour for
each app with a complication on the active watch face. This budget is
shared among all complications on the watch face. After you exhaust
the budget, the system delays your requests until more time becomes
available.
So there is no way to do this at rate of 1 per 5 min on watchOS.
One could try to do this via iOS, and send the new complication data using func transferCurrentComplicationUserInfo(_ userInfo: [String : Any] = [:]) -> WCSessionUserInfoTransfer, but the docs say:
If the complication is on the active watch face, you are given 50
transfers a day.
So I do not see any way to preload complication data at this rate before a user raises the watch.

Having complication data up to date when the user unlocks their watch?

I'm currently working on a simple app that displays data received over the network in a watchOS complication. Notably, this data is only relevant for ~30 minutes before a new network fetch is required.
I'd like to have the complication be up to date when the user unlocks their watch in the morning (this is a common use case presented by Apple).
Is it possible to receive some kind of background task when the user unlocks their watch? If I schedule a background task and the watch is locked and charging when the refresh happens, will the background task still fire? What techniques can I use to have data ready for the user when they wake up and unlock their watch? Is there documentation specifically focusing on background tasks when the watch is locked?
As far as I know, the watch works in its locked state only slightly differently from its unlock state.
One difference is the display of complications:
You can specify the privacy behaviour, i.e. what the clock face displays as complication (you can select what is displayed on the lock screen).
So, to my mind, it is possible to run background tasks as scheduled when the watch is locked and charging. Thus the data will automatically be ready when the watch is unlocked.
For this reason, there is no special documentation what happens when the watch is locked, except for some special cases, as what is displayed on the watch face in the locked state.
I would use the app life cycle docs here, and quite possibly choose:
in applicationDidEnterBackground(), I'd set a flag (time when the
complication was last updated). I'd suggest you use a singleton, so that it's accessible anywhere in your app.
then in applicationDidBecomeActive() i'd pick up the flag, compare it
with the current time, and notify the active ViewController to
refresh its data, if it's greater than 30 minutes.
if the flag doesn't exist, because the app was terminated, or it's a
first launch, then refresh anyway (set a 24h date in the past, to
use the same logic as in 1/,2/)
if you want to make it more permanent, use NSDefaults to store the last time the complication was updated.

How to run a part of Code and upDate UI even when the iPhone App is BackGround

A piece of code need to be executed even after the iPhone app sent to background.
Based on the values the UI need to get changed.
When I bring the App to foreground.It need to be updated with the changes made at the background time.
Eg.When I sent a GPS application to BackGround.It need to receive the positions accordingly and update the UI.
As far as i know, you may not update the UI while in background (As per memory guidelines, you should release any unused/cached portions of your interface). However, you can schedule your update to the moments after your app is reactivated.

How to regularly check for RSS/email-like updates online with iOS4

Is it possible to have an App (running iOS4 on hardware supporting multi-tasking) which starts on iPhone startup and then regularly checks for online updates (every 15 minutes) and then refresh the badge, so the user can see how many unread items there are with-out having a push-server?
I was hoping this would be possible with iOS4 Programming Guide seems to suggest it is only possible to to this regarding Locations tracking, VoIP and playing background audio. There is also the possibility to do local-push notifications, but I don't see to find how to trigger a specific function that way.
Thanks in advance for any help!
edit:
Just having read a bit more, if the application is in the background/inactive state, and I son want to update the badge-number without displaying a message, is there an action triggered like didReceiveLocalNotification? There must be a way to schedule something on a regular basis (like email checking) without having to implement a full server-push-nitification system!
Is it possible to have an App ... which starts on iPhone startup and then regularly checks for online updates (every 15 minutes) and then refresh the badge
As you already figured out, the answer is no.

How do I get a codeblock to run on the iPhone once every 24 hours at midnight if my app is suspended (multitasking)

Was reading through the iOS4 documentation for multitasking, and couldn't figure it out.
I basically need to update the badge count on my app's icon after midnight each day as long as the app is running in suspended mode (with multitasking).
I know this has to be possible, just can't figure out the best way to do it.
Thanks.
iOS "multitasking" is very specific. There's an important distinction between states here:
Suspended: Your app resides in memory, but does not receive any execution time. This is really only useful for fast app switching.
Background: In a few particular cases, you can request that the OS to run your app in the background. (Playing audio, location, finishing a long task, voip.)
So, the short answer to your question is, "you can't."
Here are a couple useful links on iOS multitasking, such as it is.
Understand an Application's States and Transitions
Executing Code in the Background
You could use a UILocalNotification to set the badge (without an alert) but you can't increment the badge because you're app does not have the opportunity to execute any code when the notification fires.
You can schedule up to 64 notifications in advance, one at midnight for the next 64 days, each one setting a new badge number. It would make a lot more sense to schedule a repeating notification but since your app can't execute code it can't increment the badge number that doesn't work.