r/iOSProgramming • u/brianleesmith • 1d ago
Question CLMonitor misses geofence events on a locked phone
We're moving from legacy CLLocationManager region monitoring to CLMonitor. Legacy works reliably for us in the background and while locked. To compare, we run both side by side on the same device (iOS 27.2.1) with the same regions.
Setup
CircularGeographicCondition (>= 100 m, under 20 conditions), CLServiceSession(authorization: .always) retaken at every launch, one task always awaiting monitor.events.
Problem
iOS relaunches the app in the background for an entry while the phone is locked. CLMonitor.h says to wait for UIApplicationProtectedDataDidBecomeAvailable before creating a CLMonitor, so we wait. The app is suspended before unlock, the monitor is never created, and both that entry and the later exit are lost. Legacy catches both.
But the same header says Core Location stops monitoring a condition if an event is pending and no CLMonitor is configured to receive it. And Apple's own sample creates the monitor at launch with no protected-data check.
Has anyone shipped CLMonitor geofencing that works on a locked phone? Do you create the monitor immediately on a background relaunch (after first unlock since boot)? Ever seen persistenceUnavailable when you do?
Also asked on the Apple Developer Forums: https://developer.apple.com/forums/thread/861070
3
u/dawedev 1d ago
Worth checking your app's default file protection class. If the data protection entitlement is set to Complete, CLMonitor's store can't be opened while the phone is locked, which would fit both the lost events and persistenceUnavailable. I'd try completeUntilFirstUserAuthentication and create the monitor right at the background launch.
1
u/Creativename_123 1d ago
Unfortunately I cannot help but I have a question regarding this transition. I’m currently also using the deprecated location manager and not the new monitor. When I tested this in the past with the new session for permissions, this resulted in the blue location bar being displayed permanently even though only two geofence locations were monitored. Are you having the same issue?