iOS Location permission

Ask the user for permission to access their location.

  1. Location on iOS
  2. Request Location Permissions

To protect user privacy, apps that use location services must request location permissions. Check the iOS Documentation about requesting authorization for Location Services for more details.

Location on iOS

Woosmap Geofencing SDK on iOS requires your app to get the user’s authorization to access his location data. There are two types of authorization you need to request:

Before requesting permissions, your Xcode project must be configured with specific purpose strings. The system displays these strings in the authorization request dialogs. Add following keys with purpose strings to your app’s Info.plist file

Example strings to put in Info.plist:

xml
        <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Always turning on location services allows us to gives you discount code when when you are nearby our stores.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Turning on location services allows us to guide you when you go out to pick up your order</string>

    

Finally, to receive events in the background, configure the “Location updates” and “Background fetch” capabilities for your app in Xcode, as shown below.

Location Capability iOS

Within your app code, continuous updates are enabled by setting the allowsBackgroundLocationUpdates property of the location manager to true.

swift
        private var locationManager: CLLocationManager = CLLocationManager()
locationManager.allowsBackgroundLocationUpdates = true

    

Request Location Permissions

To request foreground and background location permission you’ll need to instantiate a CLLocationManager and use first requestWhenInUseAuthorization and immediately after, requestAlwaysAuthorization

swift
        import UIKit
import CoreLocation

class ViewController: UIViewController {
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        LocationManager.shared.requestLocationAuthorization()
    }
}

class LocationManager: NSObject, CLLocationManagerDelegate {
    static let shared = LocationManager()
    private var locationManager: CLLocationManager = CLLocationManager()
    public func locationManager(_ manager: CLLocationManager,
                                didChangeAuthorization status: CLAuthorizationStatus) {
        self.requestLocationAuthorization()
    }
    public func requestLocationAuthorization() {
        self.locationManager.delegate = self
        let currentStatus = CLLocationManager.authorizationStatus()
        if currentStatus == .notDetermined {
            self.locationManager.requestWhenInUseAuthorization()
        } else if currentStatus == .authorizedWhenInUse {
            self.locationManager.requestAlwaysAuthorization()
        }
     }
}     

    
Was this article helpful?
Have more questions? Submit a request