3

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.

Luigi Piccoli
  • 33
  • 1
  • 1
  • 5
  • You can get some information about the background task and Implement the location manager But it is in Objective-C. May be useful for you. http://stackoverflow.com/questions/21440190/ios-startmonitoringsignificantlocationchanges-sometimes-breaks-monitoring – Aashish1aug Apr 03 '17 at 11:19

1 Answers1

11

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

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • Thank you very much for your answer. Indeed, my employer told me to monitor the location every X minutes but, as you say, it is not a good option. I will try doing as you say and convince him :) – Luigi Piccoli Apr 03 '17 at 11:32
  • @luigi-piccoli : Lemme conclude in simple words. If your app actually makes use of location and adds some value to user (like map app) 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. Hope am clear :). Monitoring user location at regular interval is not a option, because even if you opt background location update capability, there is no guarantee that it triggers every minute, What if user keeps his phone on desk and never moves – Sandeep Bhandari Apr 03 '17 at 11:36
  • thats why apple provided delegate pattern :) – Sandeep Bhandari Apr 03 '17 at 11:39
  • I found that if the user not move then there will be no update if I use the locationManager. How can i still get LocationUpdate when the iPhone just put on the table and not move? – user6539552 May 26 '17 at 01:21
  • @user6539552 : You cant buddy :) Location delegates trigger only when the device location changes. You can assume the device to be in its last location as long as new location delegates are not triggered – Sandeep Bhandari May 26 '17 at 05:28
  • Thanks, actually, what i want is to hope this function can be triggered from time to time, so that i can do something extra in the function :( – user6539552 May 26 '17 at 14:05
  • I tried to set the followings self.locationManager.desiredAccuracy = kCLLocationAccuracyBest self.locationManager.distanceFilter = kCLDistanceFilterNone self.locationManager.allowsBackgroundLocationUpdates = true self.locationManager.pausesLocationUpdatesAutomatically = false And then I found that it does not change at the beginning if i stay and not move. However, after i keep on moving for an hour, it keep on logging the location every seconds. Any clues? – user6539552 Jun 01 '17 at 10:28
  • @user6539552 : I guess if u have used startLocationUpdates it will continuously trigger the location changes as u have set the distance filter none. But apple will reject your app if u use startLocationUpdates and use it for doing anything other than showing users location on map or something. So be careful of the fact that u have to go through apple review as well :D – Sandeep Bhandari Jun 01 '17 at 10:44
  • My app is for research use and will not submit to app store for review. I just want to make it to forever get update (log location every minutes, even if the phone just put on the desk without moving). Any method to do this? i tried out many methods, but never succeed. :( – user6539552 Jun 01 '17 at 10:45
  • @user6539552 : Sorry but you cant buddy. StartLocationUpdates() is the best u can do considering u won't submit it to App Store. But you can't keep logging locations every minute if device never moves. But that being said I can help u to get location state every minute but I promise u can never even dream about submitting it to App Store :D – Sandeep Bhandari Jun 01 '17 at 10:52
  • @user6539552 : This is a hack and never suggested to do it. Just because u have been struggling a lot am stating it here. You cant get location every minute using location manager. What you can do though is find a way to keep ur app alive in background and then poll user location every minute :D Now how will u keep the app in background alive?? Opt for play audio background mode, play a silent audio which will ensure ur app will be kept alive as long as it plays audio file. What u can do is get some silent audio and then play it in loop – Sandeep Bhandari Jun 01 '17 at 11:00
  • @user6539552 : This will ensure ur app will be alive till either phone dies after battery drain out or user kills the app. Now add a NStimer call a method to poll user location from it for every minute. Now go to your location manager get user location from it every minute. So now u have a method to get location every minute. Take my words apple will never allow such apps – Sandeep Bhandari Jun 01 '17 at 11:04
  • thanks @SandeepBhandari again as i said, this app will NEVER submit to app store, this is not our intention. will check if possible to make it an audio playing app instead. – user6539552 Jun 01 '17 at 11:08
  • @SandeepBhandari Thanks for the valuable information. manager.allowsBackgroundLocationUpdates = true, will update location, only if the user location changes - right? So how it will drain the battery of iOS devices. – Sudhi 9135 Aug 23 '17 at 07:03