Robert
Robert

Reputation: 5849

How does this app ask for background location permission immediately?

My understanding since iOS 13 is that background location permissions can only be granted by the user after they have already granted foreground location permissions, and the app is in the background, when a location event which would have triggered the app's background location occurs. At that point they get a dialog something like:

Allow “App” to also access your location even when you are not using the app?

Every app I've used has the same behaviour, except one app, which is able to present that dialog immediately after asking for the foreground location permission dialog:

Background location permission dialog in-app

How does this app immediately and repeatedly trigger the background location dialog like this?

Upvotes: 2

Views: 2314

Answers (2)

Martin
Martin

Reputation: 4765

In my case it didn't work at first, because I tried to get permission for 'always' twice. Here's a snippet for code-lovers:

let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()

So it's important to use WhenInUse here! Then when the user grants access, you can ask for always:

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    switch status {
    case .authorizedWhenInUse, .authorized:
        locationManager.requestAlwaysAuthorization()
        fallthrough
    case .authorizedAlways:
        ... //(do your business here)

    }
}

Upvotes: 0

Paulw11
Paulw11

Reputation: 114773

If your app has asked for and received "when in use" authorisation it can then ask for "always" authorisation to trigger a second permission dialog. This behaviour requires iOS 13.4 or later.

You should consider the user experience. I suggest that your app explains why it needs always authorisation before asking for it, otherwise the user may be peppered with permission requests

Upvotes: 3

Related Questions