Is using Core Location for performing functionality in the background appropriate?

拈花ヽ惹草 提交于 2019-12-29 09:25:10

问题


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.


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/41021733/is-using-core-location-for-performing-functionality-in-the-background-appropriat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!