requestPermission method
Request permissions to access location.
Requesting access to location is a two step process on both Android and iOS:
- First, ask for using location 'when in use'
- Then, ask for using location 'always'
See the FAQ in the permission_handler plugin or the Android or iOS documentation.
Note that if the permission is PermissionStatus.permanentlyDenied, no dialog will be
shown on requestPermission. In this case, the Settings page from the
OS needs to be shown and the user needs to manually allow access to location.
The permission_handler plugin has a method named openAppSettings() which
opens the Settings page on Android / iOS.
This method is, however, NOT used by this context sampling package, since
handling of permissions should be taken care of on an app level.
Goes through SmartPhoneClientManager.requestPermissions so it never
collides with another dialog. Asks 'when in use' first, then 'always'.
Implementation
Future<PermissionStatus> requestPermission() async {
debug('$runtimeType - Requesting permission to access location...');
await SmartPhoneClientManager().requestPermissions([
Permission.locationWhenInUse,
Permission.locationAlways,
]);
final granted = await hasPermission();
if (!granted) {
warning(
"$runtimeType - The user opted not to allow collection of location data. "
"The only way to change the permission's status now is to let the "
"user manually enables it in the system settings.",
);
}
return granted ? PermissionStatus.granted : PermissionStatus.denied;
}