iOS Region Monitoring With SwiftUI
Geofencing made easy with Swift
Alright! Let’s get right into it. I will be creating a new Swift project with SwiftUI interface in Xcode. I have decided to go with the UIKit App Delegate life cycle since it will be easier to set up the core location and notification delegates for this tutorial.
Getting Started
Core Location framework’s CLLocationManager
enables developers to monitor users’ location and trigger location-related events to your app. Let’s look at how we can use this to show a notification to the user when they enter/exit a given area.
Setting up the Location Manager
We will initialize the CLLocationManager
in our SceneDelegate.swift
file. When you register a region and start monitoring, iOS will continue to monitor the regions even when the app goes to the background. If the app is not running, the operating system will awake or relaunch the app and try to deliver the event. This makes SceneDelegate
an ideal place to handle them.
In order to receive events related to region monitoring, we need to request “Always on permissions” from the user:
Register SceneDelegate.swift
to receive location manager events. Below delegate methods locationManager(_:didEnterRegion:)
and locationManager(_:didExitRegion:)
will handle the enter/exit events of our geofence. For now, you can keep the methods empty. We’ll implement them later.
Make sure to include the descriptions for NSLocationAlwaysAndWhenInUseUsageDescription
and NSLocationWhenInUseUsageDescription
keys on the info.plist
file. Without these entries, your app will not request location permissions.
If you build and run your app now, you should see your app requesting for location access like below,
Creating the Region
Now that our location manager setup is done, we can go ahead and start monitoring the region. We can define a region using CLCircularRegion
the object. A region is basically a circular area centered on given geographical coordinates. We can define a region by providing the latitude, longitude, and radius (given in meters). Radius will determine the boundary.
For the sake of this tutorial, We will be registering the region in the SceneDelegate
itself so we can start to monitor the region as soon as the app is launched.
Registering the Region
The location I am going to monitor is Times Square, New York
. You may choose any location to your liking.
In order to start monitoring, we need to register the region to our CLLocationManager.
We do that by calling startMonitoring(for:)
method.
Listening for Events
To listen to geofencing events, we are going to use the locationManager(_:didEnterRegion:)
and locationManager(_:didExitRegion:)
delegate methods we discussed earlier:
Here, we are only updating a simple Text view on our ContentView
if the app is active. If the app is not active a local notification is displayed to the user. You can decide what you want to do with your app here:
Testing your App
Testing the app is actually pretty easy. We can use a custom .GPX
file and simulate the location of the iOS simulator or the actual device you are building and running your app on.
Simply create a file with .gpx format within your Xcode project and add the above content. You can update the lat, long values to anything you like. Then launch a simulator or connect your test device and build and run your app.
After running the app, minimize it and click the simulate location button on the bottom toolbar. Now select your custom location.
After a few seconds, you should be getting a notification. If you need to test the exit event, simply select any other default locations from the list.
That’s it! You have successfully integrated region monitoring on iOS with Swift. Util next time, stay safe!
Resources
You can download the sample project from GitHub.