Run a Swift 2.0 App Forever in Background to Update Location to Server

Run a Swift 2.0 app forever in background to update location to server

Periodic location updates are a bit tricky in IOS.There's a good thread that discusses this, you can read more here

iOS will terminate your app after a few minutes, regardless if your timer is running or not. There is a way around this though, I had to do something similar when writing an ionic app so you can check out the code for this here, that link has a swift class that manages the periodic location updates in iOs.

In order to get periodic locations in the background, and not drain the battery of the device, you need to play with the accuracy of the location records, lower the accuracy of the location manager setting its desiredAccuracy to kCLLocationAccuracyThreeKilometers, then, every 60 seconds you need to change the accuracy to kCLLocationAccuracyBest, this will enable the delegate to get a new, accurate location update, then revert the accuracy back to low. The timer needs to be initialized every time an update is received.

There's also a way to wake up the app in the background after its been killed by the user, use the app delegate to have the app listen for significant changes in location before its killed. This will wake up the app in the background when the user's location makes a big jump (can be around 200ms). When the app wakes up, stop monitoring for significant changes and restart the location services as usual to continue the periodic updates.

Hope this helps.

Update

In Swift 2 you'll also need:

self.locationManager.allowsBackgroundLocationUpdates = true

how to update user location in both foreground and background every 10 seconds?

I had a similar app to be built some time ago, and I also tried things like you did, but that is very very wrong way to use timer and all to record your location.

First of all, I suggest you to not try to record/ping location explicitly because, it will cause batter drainage. There is something called, significant location change, please go through it in docs. This will trigger you location update in every single significant location change.

But I assume, you really need to ping the location. There is a git repo, link below

Background geo location tracking

Please go though it, it is really nice library out there, I have ever come across, go through its documents. I am sure you will come around and be able to use it for your app. :)

CoreLocation - Continuous location update in background with authorizedWhenInUse authorization

This probably has nothing to do with Core Location. Signal 9 indicates you’ve been killed for using too much memory or similar. You probably need to release resources before going into the background. Or you may be trying to do other things in the background that you have not told us about.

How do I get a background location update every n minutes in my iOS application?

I found a solution to implement this with the help of the Apple Developer Forums:

  • Specify location background mode
  • Create an NSTimer in the background with UIApplication:beginBackgroundTaskWithExpirationHandler:
  • When n is smaller than UIApplication:backgroundTimeRemaining it will work just fine. When n is larger, the location manager should be enabled (and disabled) again before there is no time remaining to avoid the background task being killed.

This works because location is one of the three allowed types of background execution.

Note: I lost some time by testing this in the simulator where it doesn't work. However, it works fine on my phone.

Periodic iOS background location updates

It seems that stopUpdatingLocation is what triggers the background watchdog timer, so I replaced it in didUpdateLocation with:

[self.locationManager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers];
[self.locationManager setDistanceFilter:99999];

which appears to effectively power down the GPS. The selector for the background NSTimer then becomes:

- (void) changeAccuracy {
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
}

All I'm doing is periodically toggling the accuracy to get a high-accuracy coordinate every few minutes and because the locationManager hasn't been stopped, backgroundTimeRemaining stays at its maximum value. This reduced battery consumption from ~10% per hour (with constant kCLLocationAccuracyBest in the background) to ~2% per hour on my device.



Related Topics



Leave a reply



Submit