Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreLocationManager never pauses?

Trying to get my app to use less power, it's tracking location always in the background but I'd like for it automatically pause so I can turn on region watching and use that to resume precise location monitoring once the user moves around a bit.

I've had the app on for half an hour now and the location service is not pausing. I think this has been the case since Apple changed location stuff in iOS 13? I'm not really sure. All the documentation I can find online seems extremely outdated.

Any advice is greatly appreciated, relevant code follows:

init() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 10
        locationManager.activityType = .fitness
        locationManager.requestAlwaysAuthorization()
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.pausesLocationUpdatesAutomatically = true
        locationManager.showsBackgroundLocationIndicator = true
        locationManager.startUpdatingLocation()
    }
    func locationManagerDidPauseLocationUpdates(_ manager: CLLocationManager) {
        delegate?.paused(tracker: self)
        print("MT | LOCATION SERVICES PAUSED!") <---- NEVER GETTING CALLED (been running for 40 minutes now, no location updates, still going though?)
        // if not already, start region monitoring

    }
 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        // Collect data
        for location in locations {
           print("MT | New Location: \(location)")
        }
    }
like image 654
nickneedsaname Avatar asked Feb 04 '20 02:02

nickneedsaname


1 Answers

I wouldn't rely on connecting to Xcode. Xcode is generally greedy. It doesn't want you to stop your debugging. So it never puts the app in a suspended state.

The callback of locationManagerDidPauseLocationUpdates is somewhat identical to your app being suspended which Xcode prevents.

The way I would test this is to use os.log. Then log the outputs to your Mac's console app. See here.


Just make sure the app is not launched from you running from Xcode. I'd rather disconnect your device from Xcode, kill the app. Disconnect your device from Xcode. Then just tap on the app icon again. That way Xcode cannot intervene.

like image 54
mfaani Avatar answered Oct 02 '22 17:10

mfaani