I would like to retrieve the user's current location when they are logging some information with my App Intent. When the app has been just run, this works just fine, but if it has been force quit or not run recently, the Core Location lookup times out. I have tried logging the information and using the Core Location background mode, and I can verify that the background mode is triggering because there is an indicator on the status bar, but the background mode does not seem to fire the delegate.
Is there a good way to debug this? When I run the app, everything works just fine, but I can't confirm that delegate calls are going through because I can't debug from an App Intent launch.
Here is the perform method from my App Intent
func perform() async throws -> some ProvidesDialog {
switch PersistenceController.shared.addItem(name: name, inBackground: true) {
case .success(_):
return .result(dialog: "Created new pin called \(name)")
case .failure(let error):
return .result(dialog: "There was a problem: \(error.localizedDescription)")
}
}
addItem
calls LocationManager.shared.getCurrentCoordinates
:
func getCurrentCoordinates(inBackground: Bool = false, callback: @escaping (CLLocation?) -> Void) {
if lastSeenLocation != nil {
callback(lastSeenLocation)
return
}
if inBackground {
locationManager.allowsBackgroundLocationUpdates = true
locationManager.showsBackgroundLocationIndicator = false
}
let status = CLLocationManager.authorizationStatus()
guard status == .authorizedAlways || status == .authorizedWhenInUse else {
DispatchQueue.main.async { [weak self] in
self?.callback?(nil)
self?.locationManager.allowsBackgroundLocationUpdates = false
}
return
}
self.callback = callback
locationManager.startUpdatingLocation()
}
The CLLocationManager delegate didUpdateLocations
then calls the callback
with the location and sets allowsBackgroundLocationUpdates
to false. And the callback
saves the location data to Core Data.
What is the best practice for using Core Location in an App Intent?