Background location update by timer / periodically in iOS 8

I would like to ideally update the user's location every 5 minutes if the application is in the background or in the foreground state. This application is with a very sensitive location, so knowing the location at all times is crucial.

There were many answers to this question on SO, but many of them relate to iOS 6 and earlier. After iOS 7, many background tasks changed, and I had difficulty finding a way to implement periodic location updates in the background.

+5
source share
2 answers

You want to use a CoreLocation delegate. Once you get the coordinate, stop CoreLocation, set a timer to start it in 5 minutes.

With iOS 8, you will need to set the plist entry for NSLocationWhenInUseUsageDescription and / or NSLocationAlwaysInUseDescription.

The Apple documentation is very clear on how to do this.

-(void)startUpdating{ self.locationManager = [[CLLocationManager alloc]init]; self.locationManager.delegate = self; [self.locationManager requestWhenInUseAuthorization]; [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; [self.locationManager startUpdatingLocation]; } -(void)timerFired{ [self.timer invalidate]; _timer = nil; [self.locationManager startUpdatingLocation]; } // CLLocationDelegate - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ if(locations.count){ // Optional: check error for desired accuracy self.location = locations[0]; [self.locationManager stopUpdatingLocation]; self.timer = [NSTimer scheduledTimerWithTimeInterval:60 * 5 target:self selector:@selector(timerFired) userInfo:nil repeats:NO]; } } 
+2
source

Information about it regarding Apple documentation contains quite a bit of information.

In addition, there are a few additional answers here and here with previous answers from stackoverflow. There should be enough information that should be able to help you!

0
source

Source: https://habr.com/ru/post/1203159/


All Articles