ios – The way to seize the situation occasion (enter/exit) that launched the app from terminated state?

0
1
ios – The way to seize the situation occasion (enter/exit) that launched the app from terminated state?


I’m implementing geofencing in my iOS app and working 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] isn’t nil to suggest the situation occasion is why it opened. Nice!

Nevertheless, although 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 example one other boundary occasion is triggered as a result of I’ve returned residence 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 could be very problematic and appears unintended!

Does anybody have any expertise or recommendation on whether or not they’ve had the identical subject or whether or not it is 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")

    // Test if app was launched resulting from location occasion
    if launchOptions?[UIApplication.LaunchOptionsKey.location] != nil {
        // App was launched resulting from location occasion
        log("App launched resulting from 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. Notice the launch occurred once I exited at 16:56, however no didExitRegion fired. Then at 16:57 once I entered, there isn’t a launch resulting from 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 resulting from location occasion
[2025-07-30 16:57:49.290] [INFO] didEnterRegion - sid: 7285152

LEAVE A REPLY

Please enter your comment!
Please enter your name here