async Task <bool> RequestStoragePermission() { //We always have permission on anything lower than marshmallow. if ((int)Build.VERSION.SdkInt < 23) { return(true); } var status = await Permissions.CheckStatusAsync <StoragePermission>(); if (status != PermissionStatus.Granted) { Console.WriteLine("Does not have storage permission granted, requesting."); var result = await Permissions.RequestAsync <StoragePermission>(); if (result != PermissionStatus.Granted) { Console.WriteLine("Storage permission Denied."); return(false); } } return(true); }
static async Task CheckPermissions(params string[] permissions) { //See which ones we need to request. var permissionsToRequest = new List <string>(); foreach (var permission in permissions) { var permissionStatus = PermissionStatus.Unknown; switch (permission) { case nameof(Permissions.Camera): permissionStatus = await Permissions.CheckStatusAsync <Permissions.Camera>(); break; case nameof(Permissions.Photos): permissionStatus = await Permissions.CheckStatusAsync <Permissions.Photos>(); break; case nameof(Permissions.Microphone): permissionStatus = await Permissions.CheckStatusAsync <Permissions.Microphone>(); break; } if (permissionStatus != PermissionStatus.Granted) { permissionsToRequest.Add(permission); } } //Nothing to request, Awesome! if (permissionsToRequest.Count == 0) { return; } var results = new Dictionary <string, PermissionStatus>(); foreach (var permission in permissions) { switch (permission) { case nameof(Permissions.Camera): results.Add(permission, await Permissions.RequestAsync <Permissions.Camera>()); break; case nameof(Permissions.Photos): results.Add(permission, await Permissions.RequestAsync <Permissions.Photos>()); break; case nameof(Permissions.Microphone): results.Add(permission, await Permissions.RequestAsync <Permissions.Microphone>()); break; } } //check for anything not granted, if none, Awesome! var notGranted = results.Where(r => r.Value != PermissionStatus.Granted); if (notGranted.Count() == 0) { return; } //Gunna need those permissions :( var missingPermissions = string.Join(", ", notGranted.Select(r => r.Key).ToArray()); throw new UnauthorizedAccessException($"{missingPermissions} permission(s) are required."); }
async Task <bool> RequestCameraPermissions() { //We always have permission on anything lower than marshmallow. if ((int)Build.VERSION.SdkInt < 23) { return(true); } bool checkCamera = HasPermissionInManifest(Android.Manifest.Permission.Camera); var hasStoragePermission = await Permissions.CheckStatusAsync <StoragePermission>(); var hasCameraPermission = PermissionStatus.Granted; if (checkCamera) { hasCameraPermission = await Permissions.CheckStatusAsync <Permissions.Camera>(); } var permissions = new List <string>(); var camera = nameof(Permissions.Camera); var storage = nameof(StoragePermission); if (hasCameraPermission != PermissionStatus.Granted) { permissions.Add(camera); } if (hasStoragePermission != PermissionStatus.Granted) { permissions.Add(storage); } if (permissions.Count == 0) //good to go! { return(true); } var results = new Dictionary <string, PermissionStatus>(); foreach (var permission in permissions) { switch (permission) { case nameof(Permissions.Camera): results.Add(permission, await Permissions.RequestAsync <Permissions.Camera>()); break; case nameof(StoragePermission): results.Add(permission, await Permissions.RequestAsync <StoragePermission>()); break; } } if (results.ContainsKey(storage) && results[storage] != PermissionStatus.Granted) { Console.WriteLine("Storage permission Denied."); return(false); } if (results.ContainsKey(camera) && results[camera] != PermissionStatus.Granted) { Console.WriteLine("Camera permission Denied."); return(false); } return(true); }