My current application requires permission for Camera, AudioInput (Microphone), and Storage. Since Android 6, it has become necessary to request these permissions at runtime in order to gain access. To accomplish this, the Ionic 2 Cordova Plugin "Diagnostics" is used, which can be found here.
The documentation provided seems to be incomplete as it only covers how to check the Bluetooth state without detailed information on calling permissions or handling different status scenarios.
After some experimentation, I discovered a function called diagnostic.requestRuntimePermission that could potentially be utilized as follows:
that.diagnostic.requestRuntimePermission('CAMERA')
.then((status) =>{
switch(status){
case "GRANTED":
console.log("Permission granted to use the camera");
break;
case "DENIED":
console.log("Permission denied - should we ask again?");
break;
case "DENIED_ALWAYS":
console.log("Permission permanently denied - may need alternative approach.");
break;
}
}).catch(e => console.error(e));
This code snippet is based on assumptions and might not be accurate. For instance, when checking if a camera is available:
let successCallback = (isAvailable) => { console.log('Is camera available? ' + isAvailable); };
let errorCallback = (e) => console.error(e);
that.diagnostic.isCameraAvailable().then(successCallback).catch(errorCallback);
Subsequently, obtaining the authorization status of the camera to determine whether the permission has been granted, denied, or remains unrequested:
that.diagnostic.getCameraAuthorizationStatus()
.then((state) => {
switch(state){
/*Work with the state to decide on requesting permission*/
}
}).catch(e => console.error(e));
However, uncertainty arises regarding the states returned by getCameraAuthorizationStatus and how to handle them effectively:
switch(status){
case "GRANTED":
console.log("Permission granted to use the camera");
break;
case "DENIED":
//Call PermissionRequest function here
console.log("Permission denied - should we try again?");
break;
case "DENIED_ALWAYS":
console.log("Permission permanently denied - alternatives required.");
break;
}
If you have insights on:
The specific statuses returned and how to retrieve them
The correct usage of diagnostics.requestRuntimePermission
Feel free to provide guidance or refer to the original discussion on the Ionic 2 Forum here.