Glax
Glax

Reputation: 1

Location does not update after application comes back from background

I am building an app where I want to keep track of updated user location whenever the app comes back from background.

I wrote my location tracking code in AppDelegate's didFinishLaunchingWithOptions method

//Core Location Administration
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 70
locationManager.requestAlwaysAuthorization()
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.startMonitoringVisits()
locationManager.delegate = self

Since I was not able to validate Visits, I added the standard location tracking too

locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()

I created CLLocationManagerDelegate block and added the following code

extension AppDelegate: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didVisit visit: CLVisit) {
        let clLocation = CLLocation(latitude: visit.coordinate.latitude, longitude: visit.coordinate.longitude)

        // Get location description
        AppDelegate.geoCoder.reverseGeocodeLocation(clLocation) { placemarks, _ in
            if let place = placemarks?.first {
                let description = "\(place)"
                self.newVisitReceived(visit, description: description)
            }
        }
    }

    func newVisitReceived(_ visit: CLVisit, description: String) {
        let location = Location(visit: visit, descriptionString: description)
        LErrorHandler.shared.logInfo("\(location.latitude), \(location.longitude)")
        UserModal.shared.setUserLocation(location)
        UserModal.shared.userLocationUpdated = Date()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.first  else {
            return
        }

        locationManager.stopUpdatingLocation()

        let uL = Location(location:location.coordinate, descriptionString: "")
        LErrorHandler.shared.logInfo("\(uL.latitude), \(uL.longitude)")
        UserModal.shared.setUserLocation(uL)
        UserModal.shared.userLocationUpdated = Date()
    }
}

I added this code to begin location tracking when the app comes to foreground

func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
        locationManager.startUpdatingLocation()
    }

If I am on a ViewController and the application goes back to the background, it does not refresh the location when the app comes to foreground.

Can someone suggest a better way to do this?

Upvotes: 0

Views: 471

Answers (1)

kdhingra7
kdhingra7

Reputation: 43

You should write code for location in didbecomeActive method of App delegate not in didFinishLaunchingWithOptions. Try this hope it will help.

Upvotes: 1

Related Questions