Building an app experience that changes based on location (e.g., in-store mode)

In this tutorial, we show you how to use the Radar iOS SDK and geofences to build a "RadarMart" app that defaults to an in-store mode when the user is in a store geofence.

We could also build a travel app that defaults to an on-trip mode when the user is in an airport, a real estate app that defaults to a self-tour mode when the user is at a home, and so on.

Video

Coming soon

Languages used

  • Swift

Features used

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: Import geofences

On the Geofences page, import geofences for RadarMart locations.

The CSV should include 8 columns:

  • description: A display name for the geofence. In this case, the store name.
  • tag: A group for the geofence. In this case, store.
  • externalId: An external ID for the geofence that maps to your internal database. In this case, the store ID.
  • type: The type of geofence geometry. In this case, circle.
  • radius: The radius in meters for type circle. In this case, 100.
  • coordinates: A JSON string representing a center in the format [longitude,latitude] for type circle. Note that longitude comes before latitude, a GeoJSON convention.
  • enabled: In this case, true.
  • metadata: A set of custom key-value pairs for the geofence. A JSON string representing a dictionary with up to 16 keys and values of type string, boolean, or number. In this case, {"parking": true} or {"parking": false}.
RadarMart #1,store,1,circle,100,"[-73.986752,40.703919]",true,"{""parking"":true}"
RadarMart #2,store,2,circle,100,"[-73.993156,40.700554]",true,"{""parking"":false}"
RadarMart #3,store,3,circle,100,"[-73.983295,40.697693]",true,"{""parking"":false}"

Step 3: Install the Radar iOS SDK

Install the Radar SDK using CocoaPods or Carthage (recommended) or by downloading the framework and dragging it into your project.

Initialize the SDK in your AppDelegate class with your publishable API key.

import UIKit
import RadarSDK

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

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

        return true
    }

}

Step 4: Check if the user is in a geofence, then change the UI

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 geofence, then show a message and default to the in-store tab.

Radar.trackOnce { (status, location, events, user) in
    user?.geofences?.forEach { (geofence) in
        let isInStore = user?.geofences?.contains { $0.tag == "store" }
        if isInStore {
            showInStoreMode()
        }
    }
}
@IBOutlet var tabBarController: UITabBarController?

func showInStoreMode() {
    tabBarController?.selectedIndex = 1 // in-store tab

    let alertController = UIAlertController(title: "Welcome to RadarMart!", message: nil, preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    if let viewController = tabBarController?.viewControllers?[1] {
        alertController.show(viewController, sender: self)
    }
}

Sample code

// AppDelegate.swift

import UIKit
import RadarSDK

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    @IBOutlet var tabBarController: UITabBarController?

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

        Radar.trackOnce { (status, location, events, user) in
            let isInStore = user?.geofences?.contains { $0.tag == "store" }
            if isInStore {
                showInStoreMode()
            }
        }

        return true
    }

    func showInStoreMode() {
        tabBarController?.selectedIndex = 1

        let alertController = UIAlertController(title: "Welcome to RadarMart!", message: nil, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        if let viewController = tabBarController?.viewControllers?[1] {
            alertController.show(viewController, sender: self)
        }
    }

}

Support

Have questions or feedback on this documentation? Let us know! Email us at [email protected].