iOS Region Monitoring With SwiftUI

Geofencing made easy with Swift

Sachinthana Aluvihare
Better Programming

--

Photo by William Hook on Unsplash

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.

Xcode project settings

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.

location manager property

In order to receive events related to region monitoring, we need to request “Always on permissions” from the user:

The request always on location permission

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.

location manager delegates

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,

iOS requesting for location access
requesting for location access

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.

Creating the region object

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.

registering the CLCircularRegion

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:

Updating user interface for geofence event

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.

Latitude and longitude for Times Square, New York

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.

Simulate location on Xcode

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.

Local notification message

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.

--

--

Software engineer with more than 5 years of experience in developing native iOS, Android apps