Diagnostic.requestRuntimePermission How? (Docs incomplete)

Well in my application I need to request the Permission for Camera, AudioInput (Micro) and Storage.
Since Android 6 I guess, you must request these Permissions on Runtime to gain access. So far so good.
To do this you need the Ionic 2 Cordova Plugin “Diagnostics” which can be found here:

As you can see this documentation is very incomplete. It only shows how to get the current State of Bluetooth.
Well but how do you actually call the permission? What status do you get back in order to work with the switch/case. All that information is needed but not here.

As far as I tried, I was able to figure out that there is some kind of diagnostic.requestRuntimePermission function. With this function you could do something like this:

           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 to use the camera - ask again?");
                              break;
                          case "DENIED_ALWAYS":
                              console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
                              break;
                      }
                  }).catch(e => console.error(e));

Notice: This is only a code which I think it could look like but I dont know if it works.
So lets take the Camera Permission as an example to work this out:
To check if a Camera is aviable I do this:

let successCallback = (isAvailable) => { console.log('Is available? ' + isAvailable); };
let errorCallback = (e) => console.error(e);
that.diagnostic.isCameraAvailable().then(successCallback).catch(errorCallback);

After that I have to get the AuthorizationSttus of the Camera, to know if the Permission is granted, denied or even not requested yet. This works as follows:

 that.diagnostic.getCameraAuthorizationStatus()
 .then((state) => {
   switch(state){
       /*HERE should I work with the State in order to request permission or not*/
   }
}).catch(e => console.error(e));

On this step I dont know what to do anymore because I dont know which states I get returned here.
I think it is something like this:

  switch(status){
     case "GRANTED":
        console.log("Permission granted to use the camera");
        break;
     case "DENIED":
        //Here should I now call the PermissionRequest function
        console.log("Permission denied to use the camera - ask again?");
        break;
     case "DENIED_ALWAYS":
        console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
        break;
   }

This doesnt work but I think it must be something like that.
So please help me and tell me:

  • Which Status get returned here and how do I access it

  • How do I use the diagnostics.requestRuntimePermission right?

Here is the link to the Stackoverflow Thread:

Someone on StackOverflow had the right answer for me (Thanks to David):

To answer your exact questions: According to the Ionic Native wrapper class for this plugin the response for a permission status request wille be one of the following objects properties:

permissionStatus: {
  GRANTED: string;
  DENIED: string;
  NOT_REQUESTED: string;
  DENIED_ALWAYS: string;
  RESTRICTED: string;
  GRANTED_WHEN_IN_USE: string;
};

In your Ionic class you can access it the following way, assuming you injected the ionic-native class in you constructor the following way:

constructor(private diagnostic: Diagnostic){}

You can access the permissionStatus object like this:

this.diagnostic.permissionStatus

Subsequently you can rewrite your switch-case statement:

let permissionStatus = this.diagnostic.permissionStatus;
that.diagnostic.getCameraAuthorizationStatus()
  .then((state) => {
    switch(state){
      case permissionStatus.GRANTED:
        console.log("Permission granted to use the camera");
        break;
      case permissionStatus.DENIED:
        //Here should I now call the PermissionRequest function
        console.log("Permission denied to use the camera - ask again?");
        break;
      case permissionStatus.DENIED_ALWAYS:
        console.log("Permission permanently denied to use the camera - guess 
        we won't be using it then!");
        break;
    }
}).catch(e => console.error(e));

And for requesting e.g. camera permission at runtime:

let permission = this.diagnostic.permission;
this.diagnostic.requestRuntimePermission(permission.CAMERA).then(
  success => {
    console.log('reuqestCameraAuthroization, success', success);
  },
  error => {
    console.log('reuqestCameraAuthroization, error', error);
  },
);

But I don’t think you need to manually request runtime permission in case of permissionStatus.DENIED or permissionStatus.NOT_REQUESTED because as the Ionic docs mention, getCameraAuthorizationStatus will request runtime permission by default if you do not call it with a “false” parameter.

If you are not sure how a result from a promise does look you can always log it to the console (e.g. console.log(success)). And you can always log the injectable (this.diagnostic in your case) to see which public methods and objects are available.

1 Like