iOS Location permission
Ask the user for permission to access their location.
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:
- When In Use: Your app can use all location services and receive events while the app is in use. iOS apps are considered in use when they’re in the foreground or running in the background with the background location usage indicator enabled.
- Always: Your app can use all location services and receive events even if the user is not aware that your app is running. If your app isn’t running, the system launches your app and delivers the event.
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
NSLocationWhenInUseUsageDescription
: describes the reason that the app accesses the user’s location, this is used while the app is running in the foreground.NSLocationAlwaysAndWhenInUseUsageDescription
: describes the reason that the app is requesting access to the user’s location information at all times. Use this key if your iOS app accesses location information while running in the background and foreground.
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 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.
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
requestWhenInUseAuthorization
: Location is only available when the application is in the foreground (though it continues to access it for a very short interval once the user switches to the background).requestAlwaysAuthorization
:CoreLocation
holds onto the events and asks the user at an appropriate time whether they would like to Always Allow?. After that, the location events can be received in the background as well.
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()
}
}
}