Home>

I want to be able to acquire and record location information even when the app is closed, like a running app.

The problem i am having

locationManager.allowsBackgroundLocationUpdates = trueIs described in viewDidLoad, so when you close the app for the first time, an icon with a blue arrow is displayed in a white circle, and the location information is acquired even in the background. However, when I open and close the app again, it doesn't get the location information.

When you close the app for the second time, the white circle that informs you of the acquisition of location information does not display the icon with the arrow drawn in blue, but the arrow filled in white is displayed.
AlsolocationManager (_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])Is not called.

Corresponding source code
override func viewDidLoad () {
        super.viewDidLoad ()
        locationManager = CLLocationManager ()
        // Settings that require the highest location information
        locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        // Get location information every time you move 5 meters
        locationManager.distanceFilter = 5
        locationManager.requestWhenInUseAuthorization ()
        // Make it possible to get location information even in the background
        locationManager.allowsBackgroundLocationUpdates = true
        // Do not interrupt the acquisition of location information automatically
        locationManager.pausesLocationUpdatesAutomatically = false
}
  • Answer # 1

    // Set notification when starting the application/returning to the foreground
    NotificationCenter.default.addObserver (
    self, self,
    selector: #selector (ElevationViewController.onDidBecomeActive (_ :)),
    name: UIApplication.didBecomeActiveNotification,
    object: nil
    )
    Due to this description, the location information could not be acquired when returning to the foreground.