requestPermission static method

Future<int> requestPermission()

Manually request location permission from the user with the configured Config.locationAuthorizationRequest.

The method will resolve successful if either WhenInUse or Always is authorized, regardless of Config.locationAuthorizationRequest. Otherwise an error will be returned (eg: user denies location permission).

If the user has already provided authorization for location-services, the method will resolve successfully immediately.

If iOS has already presented the location authorization dialog and the user has not currently authorized your desired Config.locationAuthorizationRequest, the SDK will present an error dialog offering to direct the user to your app's Settings screen.

⚠️ Note:

  • The SDK will already request permission from the user when you execute start, startGeofences, getCurrentPosition, etc. You do not need to explicitly execute this method with typical use-cases.

Example

initPlatformState async {
  // Listen to onProviderChange to be notified when location authorization changes occur.
  BackgroundGeolocation.onProviderChange((event) {
    print("[providerchange] $event");
  });

  // First ready the plugin with your configuration.
  let State = await BackgroundGeolocation.ready(Config(
    locationAuthorizationRequest: 'Always'
  ));

  // Manually request permission with configured locationAuthorizationRequest.
  try {
    int status = await BackgroundGeolocation.requestPermission();
    if (status == ProviderChangeEvent.AUTHORIZATION_STATUS_ALWAYS) {
      print("[requestPermission] Authorized Always $status");
    } else if (status == ProviderChangeEvent.AUTHORIZATION_STATUS_WHEN_IN_USE) {
      print("[requestPermission] Authorized WhenInUse: $status");
    }
  } catch(error) {
    print("[requestPermission] DENIED: $error");
  }
}

ℹ️ See also:

Implementation

static Future<int> requestPermission() async {
  // For future, we will accept an optional String of a specific permission to request (NOT YET IMPLEMENTED)
  // eg: "LOCATION_WHEN_IN_USE", "LOCATION_ALWAYS", "ACTIVITY_RECOGNITION"
  // @see BackgroundGeolocationModule:
  //    private void requestPermission(final String permission, final MethodChannel.Result result)
  const permission = null;
  return (await _methodChannel.invokeMethod<int>(
      'requestPermission', permission))!;
}