Search code examples
iosswiftgeolocationcore-location

How to setup a "Check-In" alert using Geofencing and CLCircularRegion?


I'm trying to set up an alert that once a user enters a specific location the alert pops up and allows users to "Check-in". After the user checks in the app then notifies an api endpoint that the user successfully checked in. This is my first time working with geofencing and corelocation. I get the basic idea of how to set it up but am not entirely sure how the check-in alert and geofence tie together. Here is my code:

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate{


  var manager = CLLocationManager()


  override func viewDidLoad() {
    super.viewDidLoad()


    // Core Location
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()


    var latitude: CLLocationDegrees = 43.039278
    var longitude: CLLocationDegrees = -87.932479
    var center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var radius: CLLocationDistance = CLLocationDistance(10.0)
    var identifier: String = "storeID"

    var geoRegion: CLCircularRegion = CLCircularRegion(center: center, radius: radius, identifier: identifier)

  }


  func showSimpleAlertWithTitle(title: String!, message: String!, viewController: UIViewController) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    let action = UIAlertAction(title: "Check-In", style: .Cancel , handler: nil)
    alert.addAction(action)
    viewController.presentViewController(alert, animated: true, completion: nil)
  }


}

Solution

    1. Set notifyOnEntry = true on the CLCircularRegion to be notified when entering or exiting the region.
    2. Implement locationManager:didEnterRegion: delegate method to handle the event.

    Example:

    override func viewDidLoad() {
        super.viewDidLoad()
    
    
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
    
        var latitude: CLLocationDegrees = 43.039278
        var longitude: CLLocationDegrees = -87.932479
        var center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
        var radius: CLLocationDistance = CLLocationDistance(10.0)
        var identifier: String = "storeID"
    
        var geoRegion: CLCircularRegion = CLCircularRegion(center: center, radius: radius, identifier: identifier)
        geoRegion.notifyOnEntry = true
    
        manager.startMonitoringForRegion(geoRegion)
    }
    
    
    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    
        showSimpleAlertWithTitle("Entered region \(region.identifier)", message: nil)
    }
    
    func showSimpleAlertWithTitle(title: String!, message: String!) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        let action = UIAlertAction(title: "Check-In", style: .Cancel , handler: nil)
        alert.addAction(action)
        presentViewController(alert, animated: true, completion: nil)
      }
    

    See: CLRegion.notifyOnEntry