IOS GPS tracking app that runs all the time

I’m trying to make an application for tracking user GPS all the time, this application is a kind of car GPS tracker to find the driver’s location all the time and send it to the server.

I tried to add "location updates" to the "background modes", but the application will automatically pause after 10 minutes when switching to the background.

Is there a way to get this app to work all the time and get the GPS location?

Thanks.

+16
ios core-location gps
source share
4 answers

You have two options here:

1) Regular location tracking.
This type of tracking works with kCLAuthorizationStatusAuthorizedWhenInUse and kCLAuthorizationStatusAuthorizedAlways . When the CLLocationManager started location tracking once, it will receive location updates in the locationManager delegate method locationManager:didUpdateLocations: application can go into a paused state, but when the location manager receives a new location, the application goes into the background and processes the new location in the delegate method. How to set up location manager:

 - (void)viewDidLoad { [super viewDidLoad]; self.locationManager = [[CLLocationManager alloc] init]; // Setup location tracker accuracy self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; // Distance filter self.locationManager.distanceFilter = 50.f; // Assign location tracker delegate self.locationManager.delegate = self; // This setup pauses location manager if location wasn't changed [self.locationManager setPausesLocationUpdatesAutomatically:YES]; // For iOS9 we have to call this method if we want to receive location updates in background mode if([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]){ [self.locationManager setAllowsBackgroundLocationUpdates:YES]; } [self.locationManager startUpdatingLocation]; } 


2) Tracking location changes.
This type of tracking only works with kCLAuthorizationStatusAuthorizedAlways authorization. It receives a new location only every 500 meters, so the distance filter and the required accuracy do not work here. An application can go into a suspended state or even be interrupted by the system, but when updating a location, the application goes into the background state and gets the location in the delegate method locationManager:didUpdateLocations:
If the application was a UIApplicationLaunchOptionsLocationKey system, it will be restarted in the background with the UIApplicationLaunchOptionsLocationKey key in the launch options in the didFinishLaunchingWithOptions application didFinishLaunchingWithOptions . How to configure this type for tracking:

 - (void)viewDidLoad { [super viewDidLoad]; self.locationManager = [[CLLocationManager alloc] init]; // Assign location tracker delegate self.locationManager.delegate = self; // For iOS9 we have to call this method if we want to receive location updates in background mode if([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]){ [self.locationManager setAllowsBackgroundLocationUpdates:YES]; } [self.locationManager startMonitoringSignificantLocationChanges]; } 


You should note that both of these methods do not guarantee that your application will not go into a suspended state.
In addition, if the application was interrupted by the user (for example, using the application switcher by scrolling), tracking in the background will not work.


UPDATE (corresponding to comments)

Here are my sample code that work for me:
For regular tracking . Run the example, provide access to the user's location, click the "Start" button to start updating the location. To check the location in the simulator, select "Debug"> "Location"> "Freeway" in the simulator menu. Now you can move the application to the background using the "Home" button (Ctrl + Shift + H). Leave the simulator for more than 10 minutes, and all this time the application will receive locations. When you return to the application, you will see red pins on the map.
For significant changes . Launch the application and test as in the previous example.
Monitoring Major changes can only be started using the [self.locationManager startMonitoringSignificantLocationChanges]; method [self.locationManager startMonitoringSignificantLocationChanges];

UPDATE (iOS 11)

Location Tracking Changes in iOS 11

iOS 11 also makes some significant changes to existing APIs. One of the affected areas is location tracking. If your application uses the location only while it is in the foreground , as most applications do, you may not have to change anything at all; however, if this is one of those applications that continuously track the location of users throughout the day, you should probably book this summer to make some changes in tracking and testing possible usage scenarios.

follow this link: https://mackuba.eu/2017/07/13/changes-to-location-tracking-in-ios-11/

+13
source share

In response to comment 1 in the solution (I can’t comment on anything yet): you do not seem to have solved the problem, as your application pauses and no longer updates the location after 10 minutes.

I had the same problem: I set setAllowsBackgroundLocationUpdates to YES and I had the NSLocationAlwaysUsageDescription key in my Info.plist , but my application also used to stop location tracking after 10 minutes.

I solved this by adding both NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription to the Info.plist file so that it looks like this:

  <key>NSLocationAlwaysUsageDescription</key> <string>This app needs to use your location</string> <key>NSLocationWhenInUseUsageDescription</key> <string>This app needs to use your location</string> 
+3
source share

install it.it take battery power but your application will start in Background.OS will not pause your application.

 [self.locationManager setPausesLocationUpdatesAutomatically:NO]; 
+3
source share

I am sure that this is useful for the author, because the question was asked in February 2016, and I give an answer in June 2019. This answer may be useful for other users.

I recently worked with the same requirement. After 2-3 weeks of hard work, I did it. For other users, I create a helper class for it. Which is available on GitHub.

Please use the HSLocationManager for your requirement. I met the same requirements in one of my projects: Capchur .

A location manager that allows you to receive background location updates every n seconds with the desired location accuracy.

Advantage:

  • The OS will never kill our application if the location manager is currently running.

  • Periodically update the location when required (range is from 2 to 170 seconds (limited by the maximum allowable background task time))

  • Customizable location accuracy and time period.

  • Low memory consumption (Singleton class)

0
source share

All Articles