Sending location to a server in the background?

Hi,


My app is configured to receive location updates while running in the background.


I need to send these updates to a server. I'd like to do it as soon as possible, but it isn't absolutely necessary.


If I receive a location update in the background, am I allowed to send the data to a server immediately using an NSURLRequest? Do I need to call "beginBackgroundTaskWithExpirationHandler" to request time for the request to complete?


Or, should I just store the location update in a local database and send it when the app is running in the foreground?


I'm assuming that I can use an expression such as "[[UIApplication sharedApplication] applicationState] != UIApplicationStateActive" in my CLLocationManagerDelegate callback to determine whether I'm getting locations in the background.


Thanks!

You are technically allowed to do this, but the way you are describing what you want to do is full of pitfalls that will not end well. Let me elaborate:


- you are technically allowed to send location data to a server, but, there is a fine line that will get you in trouble at App Review time. I can't speak for App Review, but if that is all your app will do, your chances will be slim. Your app will need to have a justifiable user facing feature to be allowed to use the 'location' background mode. But it will be best to ask this question to App Review directly. You can contact them at https://developer.apple.com/contact/app-store/


- you want to send the location to the server immediately. Looking at the code you posted in your other question, I suppose you want to use standard location updates for this. So you want to send these updates every second? Several things will go wrong:

-- your network connections will quickly fall behind

-- the amount of data you are sending in the background will break the quota for background transfers, and you will no longer be able to send anything that day.


- whether you begin a background task or not, the increased amount is still finite, and you cannot guarantee that the connection you are trying to establish and the transfer will complete before your time is out. So you should be using NSURLSession


But in more ways than one, sending location data at every update is not going to end well. It will take too long, will eat up battery, and kill the background transfer quota.


My suggestion is to think this through before diving too deep.

Thanks for the reply.


The app review issues aren't my concern at this time. I'm just looking for the correct technical answer.


As far as frequency of update, the app is only configured to receive location changes every 400 meters, so even while driving on a highway, this won't happen more often than a couple of times per minute.

The best way to do this, without running into all the issues with background limitations I noted would be to save your data and upload it when the app is in the foreground.


Do not do network transfers in the bakground unless it is crucial for your app to do so. Not only your app will hit the quotas, but you would also adversely effect the performance of other apps. Now that iOS shows how much battery each app has consumed in the background to the users, transferring in the foreground will also keep your app off the top of the list and not get blamed for battery life issues.

Sending location to a server in the background?
 
 
Q