Skip to main content
In this tutorial, we show you how to use the Radar iOS SDK and geofences to show a notification when a user enters a geofence. In this example, we show local notifications on iOS using RadarDelegate. Alternatively, you could show local notifications on Android using RadarReceiver, or send remote push notifications using a webhook or integrations with platforms like OneSignal, Braze, and Airship.

Languages used

  • Swift

Features used

Steps

1

Sign up for Radar

If you haven’t already, sign up for Radar to get your API key. You can create up to 1,000 geofences and make up to 100,000 API requests per month for free.Get API keys
2

Import geofences

On the Geofences page, create a geofence.
3

Install the Radar iOS SDK

Initialize the SDK in your AppDelegate class with your publishable API key. Make your AppDelegate implement RadarDelegate. Finally, request location permissions and start tracking:
import UIKit
import RadarSDK

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, RadarDelegate {

    let locationManager = CLLocationManager()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        Radar.initialize(publishableKey: "prj_test_pk_...")
        Radar.setDelegate(self)

        self.locationManager.requestAlwaysAuthorization()

        Radar.startTracking(trackingOptions: RadarTrackingOptions.presetResponsive)

        return true
    }

}
4

Listen for events

Implement didReceiveEvents:user: to listen for geofence entry events and show notifications:
func didReceiveEvents(_ events: [RadarEvent], user: RadarUser) {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in
    if granted {
        for event in events {
            if event.type == .userEnteredGeofence {
                let content = UNMutableNotificationContent()
                content.body = "You entered a geofence!"
                content.sound = UNNotificationSound.default
                content.categoryIdentifier = "geofence"

                let request = UNNotificationRequest(identifier: event._id, content: content, trigger: nil)
                UNUserNotificationCenter.current().add(request, withCompletionHandler: { (_) in })
            }
        }
    }
}

Sample code

// AppDelegate.swift

import UIKit
import RadarSDK

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, RadarDelegate {

    let locationManager = CLLocationManager()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        Radar.initialize(publishableKey: "prj_test_pk_...")
        Radar.setDelegate(self)

        self.locationManager.requestAlwaysAuthorization()

        Radar.startTracking(trackingOptions: RadarTrackingOptions.presetResponsive)

        return true
    }

    func didReceiveEvents(_ events: [RadarEvent], user: RadarUser) {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in
        if granted {
            for event in events {
                if event.type == .userEnteredGeofence {
                    let content = UNMutableNotificationContent()
                    content.body = "You entered a geofence!"
                    content.sound = UNNotificationSound.default
                    content.categoryIdentifier = "geofence"

                    let request = UNNotificationRequest(identifier: event._id, content: content, trigger: nil)
                    UNUserNotificationCenter.current().add(request, withCompletionHandler: { (_) in })
                }
            }
        }
    }
}

Support

Have questions or feedback on this documentation? Let us know! Contact us at radar.com/support.