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 withUIApplication:beginBackgroundTaskWithExpirationHandler:
- When
n
is smaller thanUIApplication:backgroundTimeRemaining
it will work just fine. Whenn
is larger, thelocation 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
How to Check Whether iPhone and Apple Watch Are Connected
What Does It Mean to "Weak-Link" a Framework
How to Tell If Your Code Is Running on an iPhone or an Iphone3G
Nsurlconnection Deprecated in iOS9
Hide Navigationbar When Scrolling Tableview in Collectionview
Memory Leak with Large Core Data Batch Insert in Swift
How to Create and Send the JSON Data to Server Using Swift Language
How to Get the Build Uuid in Runtime and the Image Base Address
Limit Uitextfield Input to Numbers in Swift
Ios: How to Copy HTML into the Cut-Paste Buffer
How to Find Indexpath for Tapped Button in Tableview Using Seque
Firebase for iOS, Googleservice-Info.Plist Property "Is_Analytics_Enabled" Set to "No"
Post Request with Data in Body with Alamofire 4