Posts

Post not yet marked as solved
0 Replies
895 Views
I would like to clarify one thing about didUpdateLocations in background. In our app we use GPS tracking in the background. We use CLLocationManagerDelegate's didUpdateLocations method which contains a network call. We want to make sure that network call works in the background too. For example in this sample code we call ping() when didUpdateLocations triggers. During our tests it worked all the time even in background mode. import UIKit import CoreLocation class ViewController: UIViewController { let locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() if CLLocationManager.locationServicesEnabled() { locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.activityType = .automotiveNavigation locationManager.allowsBackgroundLocationUpdates = true locationManager.pausesLocationUpdatesAutomatically = false checkLocationAuthorization() locationManager.requestAlwaysAuthorization() } } func ping(url: String = "https://www.google.com") { URLSession.shared.dataTask(with: URL(string: url)!) { (_, response, _) in if let response = response as? HTTPURLResponse { print(response.statusCode) } }.resume() } func checkLocationAuthorization() { switch CLLocationManager.authorizationStatus() { case .authorizedAlways: locationManager.startUpdatingLocation() default: break } } } extension ViewController: CLLocationManagerDelegate { func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard let location = locations.last else { return } print(location.timestamp) ping() } func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { checkLocationAuthorization() } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print(error.localizedDescription) } } So our question is the following: are network calls (even in background mode) supported in didUpdateLocations or there are some cases when it will not work? We want to make sure that's work every time.
Posted Last updated
.