I have implemented geofencing using CLMonitor. The implementation follows this general structure:
private var monitorTask: Task<Void, Never>?
private var backgroundSession: CLBackgroundActivitySession?
func start() async {
backgroundSession = CLBackgroundActivitySession()
monitorTask = Task {
do {
let monitor = await CLMonitor("monitor")
for try await event in await monitor.events {
handleEvent(event: event)
}
} catch {}
}
}
func addSpot() async {
let monitor = await CLMonitor("monitor")
let center = CLLocationCoordinate2D(latitude: 0, longitude: 0)
let condition = CLMonitor.CircularGeographicCondition(center: center, radius: 100)
await monitor.add(condition, identifier: "sample-1")
}
When the app is not task-killed, the code inside handleEvent executes as expected. However, after a user-initiated task kill, the functionality does not work properly.