1

Lately, I've notice that there are a few applications request to access the user's location for performing some functionalities in the background.

For example: Application for scanning and uploading the user's photos for backup purposes, so when entering the background state, it keeps scanning and uploading.

What am I asking:

  • If there are Background Execution mechanisms for executing Background Tasks (Select Target -> Capabilities -> Background Modes), so why using the Core Location for doing such a thing?

  • If using Core Location is different, what is the benefit of using it?

Also, I've read (and this is what I assume) that using the core location for not what is meant should causes to let the application to be rejected, the weird thing that -as I mentioned- there are a few applications doing this! I feel a little confused about it.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143

2 Answers2

1

Also, I've read (and this is what I assume) that using the core location for not what is meant should causes to let the application to be rejected, the weird thing that -as I mentioned- there are a few applications doing this! I feel a little confused about it.

You are right to be in doubt. Do not imitate this behavior. These people are misusing CoreLocation as a way of getting their code to run in the background even though they are not really using any CoreLocation features.

You are not allowed to do things arbitrarily in the background — and with good reason. Don't violate the rules. If you want to keep uploading even when in the background, use a URLSession with a background URLSessionConfiguration. Do things the right way.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks! it seems logical to me, but I was wondering if using URLSessionConfiguration even the right way gives the same ability as using core location (if I'm not mistaking, it let the app runs forever in the background). And yes, even if it's not, we should stick to the rules... I am asking because I believe that there is somehow limitation(s) when doing this – Ahmad F Dec 07 '16 at 16:43
  • 1
    Background URLSessionConfiguration means that your app does _not_ run in the background. The system does the work for you, when it has time. Thus, things are as efficient as possible, because the system makes all the necessary judgments. – matt Dec 07 '16 at 16:48
0

Edit: To remove any doubt: I do not recommend the following approach, in fact, like @matt I highly discourage anyone from using this or a similar approach. Please see my comment below for a better way to update the app in the background.

To answer the OP question: You can ask CLLocationManager to get significantLocationChanges. This will not only notify you and give you CPU time in the background, but will actually launch your app (even after phone restart!) to let you know of the location changes.

This is a pretty good way of promising yourself some extra processing time in the background (although like you said - it's a very bad "app behavior" and may even cause your app to get rejected from the AppStore)

AFAIK the first app to do this was Dropbox - they had a whole screen in settings explaining why they ask you for location just to backup your images... since iOS doesn't have any event (for 3rd party devs) for informing an app on changes in the device photos, Dropbox solution was to get "woken" on those location changes and check themselves for changes, if they found any - they would upload the new images to Dropbox in the background.

significantLocationChanges only uses low-power methods such as cellular towers and nearby Wifi and therefore is pretty easy on the battery. The processing itself however can be intensive, depending on the app that takes advantage of this method.

Aviel Gross
  • 9,770
  • 3
  • 52
  • 62
  • Thank you for your time and consideration. Referring to Dropbox's application, do you think that it keeps doing this until this moment? or it changed to another "elegant" approach to do the same functionality? – Ahmad F Dec 07 '16 at 16:35
  • 1
    I believe they stopped doing that, and like @matt said - it's highly advisable that you or anyone would not implement such approach. Another solution is using background fetch - which exists since iOS7 and gives you CPU time in the background when the system feels it can do so without compromising the user's usage experience/battery/etc. That is a much cleaner approach to update data in the background. – Aviel Gross Dec 07 '16 at 16:45