I am trying to make an app to track the user GPS all the time, this app is a kind of car GPS tracker to get the location of driver all the time and send it to server.
I have tried to add "location updates" to the "background modes" but the app will automatically suspends after 10 mins when going into background.
Is there a way to make this app run all the time and get the GPS location?
Thank you.
In reply to comment 1 in the solution (I can't comment anywhere yet): you didn't seem to solve the problem as your app gets suspended and doesn't update the location any more after 10 minutes.
I had the same issue: I had set
setAllowsBackgroundLocationUpdates
toYES
, and I had theNSLocationAlwaysUsageDescription
key in myInfo.plist
, but my App also used to stop tracking location after 10 minutes.I solved it by adding both
NSLocationAlwaysUsageDescription
andNSLocationWhenInUseUsageDescription
to theInfo.plist
file so it looks like this:set it.it take power of battery but your application run in Background.OS does not Suspend your App
You have two options here:
1) Regular location tracking.
This type of tracking works with
kCLAuthorizationStatusAuthorizedWhenInUse
andkCLAuthorizationStatusAuthorizedAlways
authorizations. WhenCLLocationManager
started tracking location once it will receive location updates in delegate methodlocationManager:didUpdateLocations:
. App can go to suspended state, but when location manager receive new location app goes to background state and handles new location in delegate method. How to setup location manager:2) Signification location changes tracking.
This type of tracking works only with
kCLAuthorizationStatusAuthorizedAlways
authorization. It receives new location only each 500 meters, so distance filter and desiredAccuracy don't work here. App can go to suspended state, and even can be terminated by system, but when location updates app goes to background state and receives location in delegate methodlocationManager:didUpdateLocations:
.If app was terminated by system, it will be relaunched in background with
UIApplicationLaunchOptionsLocationKey
key in launch options indidFinishLaunchingWithOptions
app delegate method. How to setup this type on tracking:You should notice that both of these methods does not guarantee that your application does not go to suspended state.
Also, if app was terminated by user (for example from app switcher by swipe) location tracking in background will not work.
UPDATE (corresponding to comments)
Here is my code examples that work for me:
For Regular tracking. Run the example, provide access to user location, tap Start button to start location updates. To test locations in simulator choose in simulator menu Debug > Location > Freeway Drive. Now you can push app to background by home button (Command+Shift+H). Leave simulator for more than 10 minutes, and all this time app will receive locations. When you return to app you will see red pins on the map.
For Significant changes. Run the app and test by the same way as for previous example.
Monitoring Significant changes can be started only by method
[self.locationManager startMonitoringSignificantLocationChanges];
UPDATE (iOS 11)
Changes to location tracking in iOS 11
iOS 11 also makes some major changes to existing APIs. One of the affected areas is location tracking. If your app only uses location while the app is in the foreground, as most apps do, you might not have to change anything at all; however, if it’s one of those apps that continuously track user’s location throughout the day, you should probably book some time this summer for making some changes in how you do the tracking and testing possible usage scenarios.
follow this link: https://mackuba.eu/2017/07/13/changes-to-location-tracking-in-ios-11/