iOS Location permission

Ask the user for permission to access their location.

  1. Location on iOS
  2. Request Location Permissions
  3. Start Location Tracking
  4. Refresh Location with high frequency

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:

<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 when you go out to pick up your your order</string>

Finally, to receive events in the background, configure the “Location updates” capability 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.

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

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()
        }
     }
}     

Start Location Tracking

Once you have initialized the SDK and the user has authorized background permissions , you can start tracking the user’s location in the background.

To start tracking, call:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    WoosmapGeofencing.shared.startTracking(configurationProfile: ConfigurationProfile.liveTracking)
}

To stop tracking, call:

WoosmapGeofencing.shared.stopTracking();

The SDK supports three tracking profiles presets built to best meet your use cases.

Refresh Location with high frequency

Some use cases are more demanding on position collection or Geofence detection. To complete standard mode the SDK provides a high frequency location mode. This mode allows to retrieve a maximum of location collection. It also gains a more accurate detection of Geofences crossings. This mode can be set using the following method:

WoosmapGeofencing.shared.setModeHighfrequencyLocation(enable: true)

When activated, the SDK records user’s location 10 meters. For battery and performance optimisation the SDK stop POI detection, Distance calculations and Zone Classification when enabling High Frequency. This mode can be used in background or when the app has been killed. Because the High Frequency Location mode generates high battery consumption, it should be used for proper use cases, when user is well aware of the tracking and be turned off as soon as high frequent positioning is not useful anymore.

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