I’m implementing geofencing in my iOS app and operating into some issues when the app is terminated (swiped up). I’m utilizing locationManager.startMonitoring(for: area)
to provoke monitoring and utilizing locationManager(_ supervisor: CLLocationManager, didEnterRegion area: CLRegion)
and didExitRegion
to seize when the area boundary is crossed.
When the app is terminated and the primary set off comes by way of, the app launches within the background in response to the situation occasion. This triggers AppDelegate
‘s didFinishLaunchingWithOptions
and I can see that launchOptions?[UIApplication.LaunchOptionsKey.location]
shouldn’t be nil to suggest the situation occasion is why it opened. Nice!
Nevertheless, despite the fact that the very first line of code in didFinishLaunchingWithOptions
is locationManager.delegate = self
, it is as if it is too late to seize the precise enter/exit occasion and neither didEnterRegion
nor didExitRegion
are fired.
Now as an instance one other boundary occasion is triggered as a result of I’ve returned house with out touching the app. This second (and all subsequent) occasions will set off didEnter/ExitRegion
as a result of the app continues to be semi-launched from the primary occasion. So I can deal with these efficiently. However “lacking” that first occasion may be very problematic and appears unintended!
Does anybody have any expertise or recommendation on whether or not they’ve had the identical difficulty or whether or not this can be a mistake on my finish?
Thanks!
Related, simplified code inside AppDelegate
:
var locationManager: CLLocationManager = {
let locationManager = CLLocationManager()
locationManager.allowsBackgroundLocationUpdates = true
return locationManager
}()
func locationManager(_ supervisor: CLLocationManager, didEnterRegion area: CLRegion) {
log("didEnterRegion - sid: (area.identifier)")
}
func locationManager(_ supervisor: CLLocationManager, didExitRegion area: CLRegion) {
log("didExitRegion - sid: (area.identifier)")
}
func startMonitoring() {
area.notifyOnEntry = true
area.notifyOnExit = true
locationManager.startMonitoring(for: area)
log("Began monitoring")
}
func utility(_ utility: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
locationManager.delegate = self
log("LAUNCH")
// Examine if app was launched attributable to location occasion
if launchOptions?[UIApplication.LaunchOptionsKey.location] != nil {
// App was launched attributable to location occasion
log("App launched attributable to location occasion")
} else {
startMonitoring()
}
return true
}
And instance log output after terminating the app, then strolling out of the boundary after which strolling again contained in the boundary. Be aware the launch occurred after I exited at 16:56, however no didExitRegion
fired. Then at 16:57 after I entered, there is no such thing as a launch attributable to it already launching and I efficiently obtain didEnterRegion
:
[2025-07-30 16:56:05.392] [INFO] LAUNCH
[2025-07-30 16:56:05.393] [INFO] App launched attributable to location occasion
[2025-07-30 16:57:49.290] [INFO] didEnterRegion - sid: 7285152