requestLocationServiceAuthorization method

  1. @Deprecated('Plugin users should use the permission_handler plugin to request permissions. ' 'See README.md for more details.')
Future<LocationAuthorizationStatus> requestLocationServiceAuthorization({
  1. bool requestAlwaysLocationUsage = false,
})

Request to authorize the location service (Only on iOS).

This method will throw a PlatformException on Android.

Returns a LocationAuthorizationStatus after user authorized or denied the location on this request.

If the location information needs to be accessible all the time, set requestAlwaysLocationUsage to true. If user has already granted a LocationAuthorizationStatus.authorizedWhenInUse prior to requesting an "always" access, it will return LocationAuthorizationStatus.denied.

If the location service authorization is not determined prior to making this call, a platform standard UI of requesting a location service will pop up. This UI will only show once unless the user re-install the app to their phone which resets the location service authorization to not determined.

This method is a helper to get the location authorization that is necessary for certain functionality of this plugin. It can be replaced with other permission handling code/plugin if preferred. To request location authorization, make sure to add the following keys to your Info.plist file, located in <project root>/ios/Runner/Info.plist:

  • NSLocationAlwaysAndWhenInUseUsageDescription - describe why the app needs access to the user’s location information all the time (foreground and background). This is called Privacy - Location Always and When In Use Usage Description in the visual editor.
  • NSLocationWhenInUseUsageDescription - describe why the app needs access to the user’s location information when the app is running in the foreground. This is called Privacy - Location When In Use Usage Description in the visual editor.

Starting from iOS 13, getWifiBSSID and getWifiIP will only work properly if:

  • The app uses Core Location, and has the user’s authorization to use location information.
  • The app uses the NEHotspotConfiguration API to configure the current Wi-Fi network.
  • The app has active VPN configurations installed.

If the app falls into the first category, call this method before calling getWifiBSSID or getWifiIP. For example,

if (Platform.isIOS) {
  LocationAuthorizationStatus status = await _networkInfo.getLocationServiceAuthorization();
  if (status == LocationAuthorizationStatus.notDetermined) {
    status = await _networkInfo.requestLocationServiceAuthorization();
  }
  if (status == LocationAuthorizationStatus.authorizedAlways || status == LocationAuthorizationStatus.authorizedWhenInUse) {
    wifiBSSID = await _networkInfo.getWifiName();
  } else {
    print('location service is not authorized, the data might not be correct');
    wifiBSSID = await _networkInfo.getWifiName();
  }
} else {
  wifiBSSID = await _networkInfo.getWifiName();
}

Ideally, a location service authorization should only be requested if the current authorization status is not determined.

See also getLocationServiceAuthorization to obtain current location service status.

Implementation

@Deprecated(
    'Plugin users should use the permission_handler plugin to request permissions. '
    'See README.md for more details.')
Future<LocationAuthorizationStatus> requestLocationServiceAuthorization({
  bool requestAlwaysLocationUsage = false,
}) {
  return _platform.requestLocationServiceAuthorization(
    requestAlwaysLocationUsage: requestAlwaysLocationUsage,
  );
}