Skip to main content

Building an on-premise mode

In this tutorial, we show you how to use geofences in Radar to show an on-premise mode when a user is in a geofence.

Steps#

Step 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

Step 2: Create geofences#

There are several ways to create geofences:

  1. Create geofence page
  2. CSV import
  3. API

The tag is a group for the geofence. For example, you could set tag = venue for gaming, or tag = restaurant for restaurants. In this tutorial, we're going to use tag = store.

Step 3: Initialize the SDK#

Initialize the SDK with your publishable API key.

import UIKitimport RadarSDK
@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {    Radar.initialize(publishableKey: "prj_test_pk_...")    Radar.setUserId("foo")
    return true  }
}

Step 4: Request foreground permissions#

import UIKitimport RadarSDKimport CoreLocation
@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
  let locationManager = CLLocationManager()
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {    Radar.initialize(publishableKey: "prj_test_pk_...")    Radar.setUserId("foo")
    self.locationManager.delegate = self    self.requestLocationPermissions()
    return true  }
  func requestLocationPermissions() {    let status = self.locationManager.authorizationStatus
    if status == .notDetermined {      self.locationManager.requestWhenInUseAuthorization()    }  }
}

Step 5: Change the UI when the user is in a geofence#

Call Radar.trackOnce() to track the user's location in the foreground. In the callback, check user.geofences to determine if the user is in a store, then change the UI to show the on-premise experience.

import UIKitimport RadarSDKimport CoreLocation
@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
  let locationManager = CLLocationManager()
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {    Radar.initialize(publishableKey: "prj_test_pk_...")    Radar.setUserId("foo")
    self.locationManager.delegate = self    self.requestLocationPermissions()
    Radar.trackOnce { (status, location, events, user) in      let isInStore = user?.geofences?.contains { $0.tag == "store" }      if isInStore {        // show on-premise mode      }    }
    return true  }
  func requestLocationPermissions() {    let status = self.locationManager.authorizationStatus
    if status == .notDetermined {      self.locationManager.requestWhenInUseAuthorization()    }  }
}

Support#

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