In Android Marshmallow users grant permissons to apps while the app is runing,not when they install the app so how to check and grant Permissons at Run-Time in ionic ?
Asked
Active
Viewed 2.2k times
2 Answers
33
You can use cordova-diagnostic-plugin to check and request Android runtime permissions:
Check a permission:
cordova.plugins.diagnostic.getPermissionAuthorizationStatus(function(status){
switch(status){
case cordova.plugins.diagnostic.runtimePermissionStatus.GRANTED:
console.log("Permission granted to use the camera");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.NOT_REQUESTED:
console.log("Permission to use the camera has not been requested yet");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED:
console.log("Permission denied to use the camera - ask again?");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
break;
}
}, function(error){
console.error("The following error occurred: "+error);
}, cordova.plugins.diagnostic.runtimePermission.CAMERA);
Request a permission:
cordova.plugins.diagnostic.requestRuntimePermission(function(status){
switch(status){
case cordova.plugins.diagnostic.runtimePermissionStatus.GRANTED:
console.log("Permission granted to use the camera");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.NOT_REQUESTED:
console.log("Permission to use the camera has not been requested yet");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED:
console.log("Permission denied to use the camera - ask again?");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
break;
}
}, function(error){
console.error("The following error occurred: "+error);
}, cordova.plugins.diagnostic.runtimePermission.CAMERA);

DaveAlden
- 30,083
- 11
- 93
- 155
-
Hi Dave -- the success callback here just takes a status parameter. What if I want to take some other action on success, like saving a file? It seems the natural place to do that would be inside the callback function, because you want to wait till permissions are confirmed before saving, but that's not an option. Should I be storing the status in a global parameter (yuk)? Is there a better approach? – William Whyte Feb 03 '17 at 11:47
-
Json array `['CAMERA']` instead of `cordova.plugins.diagnostic.runtimePermission.CAMERA` else it fails. – Mar 31 '17 at 10:21
-
1??? surely `cordova.plugins.diagnostic.runtimePermission.CAMERA === cordova.plugins.diagnostic.runtimePermission['CAMERA']` – DaveAlden Mar 31 '17 at 10:24
-
NO sir, there was BUG. when i request check i have to use ['CAMERA'] but when 'Request a permission:' then i have to use `cordova.plugins.diagnostic.runtimePermission.CAMERA` – Mar 31 '17 at 10:28
-
I will post in issue tracker. later. – Mar 31 '17 at 10:28
-
1But in Javascript, those two forms are programmatically identical: https://jsfiddle.net/dpa99c/k2460h8u/ – DaveAlden Mar 31 '17 at 10:30
-
http://paste.ubuntu.com/24287285/ - Strange but this is how i fixed it temporary. line 42 for reading, line 66 for writing. – Mar 31 '17 at 10:34
-
Since the constant [is defined](https://github.com/dpa99c/cordova-diagnostic-plugin/blob/master/www/android/diagnostic.js#L45) in the exported plugin module, most likely you are referencing the plugin before the `deviceready` event has fired and therefore before the module has loaded, since Cordova dynamically loads the JS components of plugins at runtime. – DaveAlden Mar 31 '17 at 10:46
-
@DaveAlden sorry for the late post, but if you decided to use this, how can it be debugged? are you able to test something like this on ionic view? – James N Jun 26 '17 at 20:48
-
1@JamesN it's a native plugin, won't work with Ionic View which is an HTML5 wrapper. To debug, build a native Android app from your Cordova project then connect Chrome DevTools to the remote Webview on the device. – DaveAlden Jun 27 '17 at 06:47
-
Just for others reaching this solution, this is the only thing that actually worked to request pernissions. But be aware of some dependencies and changes in the code samples. For an updated version of this, check my response. – VictorEspina Feb 05 '21 at 03:50
0
DaveAlden solution is the only one that worked for me. Beware that this plugin is based on AndroidX, so you have to install the following files:
ionic cordova plugin add cordova-plugin-androidx
ionic cordova plugin add cordova-plugin-androidx-adapter
and then you can install the diagnostic plugin files:
ionic cordova plugin add cordova.plugins.diagnostic
npm install @ionic-native/diagnostic
Also remember to add Diagnostic to the providers list on app.module:
import { Diagnostic } from '@ionic-native/diagnostic/ngx';
...
providers: [..., Diagnostic]
Finally, the diagnostic plugin's API has changed since Dave's answer, so this is the current working code:
To check if a permission is granted:
this.diagnostic.getPermissionAuthorizationStatus(this.diagnostic.permission.CAMERA)
.then((status) => {
switch(status){
case this.diagnostic.permissionStatus.GRANTED:
console.log("Permission granted to use the camera");
break;
case this.diagnostic.permissionStatus.NOT_REQUESTED:
console.log("Permission to use the camera has not been requested yet");
break;
case this.diagnostic.permissionStatus.DENIED:
console.log("Permission denied to use the camera - ask again?");
break;
case this.diagnostic.permissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
break;
}
})
.catch((error) => {
console.error("The following error occurred: "+error);
});
to request a permission:
this.diagnostic.requestRuntimePermission(this.diagnostic.permission.CAMERA)
.then((status) => {
switch(status){
case this.diagnostic.permissionStatus.GRANTED:
console.log("Permission granted to use the camera");
break;
case this.diagnostic.permissionStatus.NOT_REQUESTED:
console.log("Permission to use the camera has not been requested yet");
break;
case this.diagnostic.permissionStatus.DENIED:
console.log("Permission denied to use the camera - ask again?");
break;
case this.diagnostic.permissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
break;
}
})
.catch((error) => {
console.error("The following error occurred: "+error);
});

VictorEspina
- 352
- 2
- 7