Geofencing

Prev Next

Overview

Geofencing in iOS enables apps to monitor specific geographic regions and trigger events or notifications when users enter or exit these areas. This feature is ideal for location-aware marketing or context-sensitive behaviors.

Note:

This page describes Apple's standard Core Location geofencing API. The Mapp Engage iOS SDK does not provide a server-side geofence trigger — geofence regions are defined and monitored entirely in your app. To send a Mapp Engage push from a geofence transition, capture the entry/exit event in your CLLocationManagerDelegate and report it to Mapp Engage as a custom attribute or event.

Use Case

  1. Purpose:

    • Trigger actions or notifications based on a user's proximity to a predefined location.

    • Deliver personalized, location-specific content to enhance engagement.

  2. Benefits:

    • Increase user interaction with context-aware notifications.

    • Provide timely and relevant updates based on user location.

Implementation Details

  1. Permissions:

    • Add the required permissions in the Info.plist file:

    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>We need your location to provide location-based features.</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>We need your location to provide location-based features.</string>
    • Request location permissions dynamically in your app:

    locationManager.requestAlwaysAuthorization()
  2. Setup Geofencing: Define a geofence using CLCircularRegion:

    let geofenceRegion = CLCircularRegion(center: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194),
                                          radius: 100,
                                          identifier: "GeofenceID")
    geofenceRegion.notifyOnEntry = true
    geofenceRegion.notifyOnExit = true
  3. Start Monitoring Geofences: Use CLLocationManager to monitor the defined geofence:

    locationManager.startMonitoring(for: geofenceRegion)
  4. Handle Geofence Events: Implement the CLLocationManagerDelegate methods to handle geofence transitions:

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        if region is CLCircularRegion {
            print("Entered geofence: \(region.identifier)")
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        if region is CLCircularRegion {
            print("Exited geofence: \(region.identifier)")
        }
    }

Keep in mind:

  • Ensure users grant appropriate location permissions (Always or When in Use).

  • Test geofencing accuracy under different conditions to optimize performance.

  • Be mindful of battery usage when monitoring geofences, especially with multiple regions.