Source: https://developers.woosmap.com/products/geofencing-sdk/ios-sdk/concepts/location-permission/

> For clean Markdown of any page, append `.md` to the page URL.

> For a complete documentation index, see https://developers.woosmap.com/llms.txt

# iOS Location permission



To protect user privacy, apps that use location services must request location permissions. Check
the [iOS Documentation about requesting authorization for Location Services](https://developer.apple.com/documentation/corelocation/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`:

```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](/assets/images/geofencing-sdk/background-location-fetch-ios-xcode.jpg)

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`

- `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.

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