I’m building a content blocker app for iOS.
Is there any way I can update the blockerList.json file from a server periodically in the background?
I have no idea how to do this, or even where to start.
If, and only if, a background task improves user experience you can declare an iOS app to have a task that runs in the "background". That is, even when another app is in the foreground, your app could potentially perform tasks "in the background".
However, be very picky about that requirement - often enough, it's not really necessary to do this. In your case for example, you could load the list when your app will move to the foreground, and then periodically while it is in the foreground. Doing things in the background when it is not really necessary will drain battery for nothing - one of the bad habits users hate the most!
Well, lets assume you have good reasons for doing it anyway ;)
An iOS app which has not declared such kind of background task will stop executing shortly after it ceases to run in the foreground. In order to support apps that really have to do something in the background - that is, when this app is not in the foreground - there are a couple of certain "background execution modes" (UIBackgroundModes
) which can be used to declare that your app wants to execute such kind of task in the background.
So, the first is to find an appropriate "background execution mode" suitable for your background task and declare that in the Info.plist
of your app. (In your case, the suitable mode would be fetch
: "The app regularly downloads and processes small amounts of content from the network.")
The next thing is to implement the task and the necessary hooks (uhm, I mean delegates), that is in your case you need to implement application:performFetchWithCompletionHandler
. You also need to deal with the "application state transitions".
Here are a few pointers to the Apple docs: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
And here is a tutorial: http://hayageek.com/ios-background-fetch/