Search code examples
c#iosgeolocationmauiios17

CLMonitor correct way to upgrade from CLLocationManager


I'm trying to upgrade geolocation functions from CLLocationManager to CLMonitor. The main question is how I supposed to move MonitoredRegions from CLLocationManager to CLMonitor. Since ios17 functions (Start/Stop Monitoring) from CLLocationManager are deprecated so How I should move that conditions if in same time I shouldn't use StopMonitoring to remove it from CLLocationManager.

CLMonitor doesn't have conditions added in CLLocationManager. When I add something to CLMonitor then in CLLocationManager we also see this condition in MonitoredRegions but with auto-generated name (some_generated_chars@identifier)


Solution

  • Deprecated doesn't mean it doesn't work, just that there is a new/better way of doing something. Deprecated capabilities will eventually disappear, but it typically takes years.

    It is up to you whether you move to CLMonitor or just keep using CLLocationManager. If your app targets versions earlier than iOS 17 you can probably just stick with CLLocationManager rather than having multiple code paths.

    CLMonitor offers a more modern, closure-based interface compared to the delegate pattern adopted by CLLocationManager. This is more "Swifty" and it also works with Swift async/await - I am not sure if this has much of an impact as you are, presumably, using Maui based on the question tags.

    However, if you did want to change your code to use CLMonitor on iOS 17 you would adopt the following approach

    • On launch on an iOS 17 device, before you set up CLMonitor, check for a boolean value in User Defaults - Something like geoFenceUpdated.
    • If it is true, do nothing.
    • If it is false then create a CLLocationManager, remove all geofences and set geoFenceUpdated to true
    • Finally, create your CLMonitor and create any required geofences.