Best way to use background location updates in iOS

2019-01-20 06:21发布

问题:

I have to make an app in iOS using Swift 3 that sends the user's location via API every 3 or 5 minutes, while the app is backgrounded. I don't know which is the best way to do this, or if it's even possible because of Apple's strict background rules. The location has to be quite precise and the app needs to run for a long time backgrounded, possibly not consuming too much battery. If it isn't possible, it would be nice to know which would be the best approach in this case. Thank you very much.

回答1:

that sends the user's location via API every 3 or 5 minutes, while the app is backgrounded

Does not make much sense. What if user stands in same location for 5 minutes? you will make same location entry in your server multiple times? Isn't it better rather than updating location at certain interval, if you could update your server with user location once user's location changes??

So use locationManager, set its delegates and start updating your server via API when user location changes rather than at regular interval.

I don't know which is the best way to do this, or if it's even possible because of Apple's strict background rules

Its absolutely possible. All you have to do is to opt for Location Updates capability in Xcode.

Now whats the best way? Its a relative term. This depends on how your app will use the users location info. If you unnecessarily start observing users location, you will unnecessarily drain the users iPhone battery, hence apple will reject your app.

How apple process's app using location update capability?

Its pretty simple. Location updates capability comes with the cost, that if your app observes user location updates accurately in background, it will drain out the device battery. Because its a costly trade off, apple expects you to use this only if its necessary for your app.

For example : If you are making a map app or an app that tracks the users location changes and then later plots on a map or something and lets the user know about his movement (like running apps) its absolutely fine to use location update capability because you are adding value to user.

If you try to think creepy! and try to use the location update just to keep your app alive and do some thing completely unrelated to location update in background app will reject your app. Like few developers try to be over smart and use location updates to keep their app in sync with server or to upload/download files at regular interval or something like that which are no way related to location updates, apple will reject such apps.

So no way to be creepy? And use location updates to do something useful which is not related to location itself?

Yes, you can. Apple is generous enough to allow that. For example : Dropbox uploads your images in background, when you move from one location to another. All it does is, it looks for user location changes, once location changes delegates triggers, creates a upload task with background session and starts uploading files.

How to do that?

You can still use the location manager. All you have to do is to use

locationManager.startMonitoringSignificantLocationChanges()

This will trigger your location delegates only when there is a significant location changes :)

On the other hand, if your app actually makes use of user locations and use

startLocationUpdates()

make sure you dont consume location updates unnecessarily. Make sure u put distance filter properly according to your apps requirements. Dont be greedy and dont waste iOS resources unnecessarily and apple will be happy and will not trouble you :)

Conclusion:

If your app actually makes use of location and adds some value to user (like map app/running apps/travel apps) only then use startLocationUpdates and use distance filters properly (optional, but good to have it).

If you are being creepy always use startMonitoringSignificantLocationChanges else app is bound to get rejected