Ionic Android permission always returns false

Viewed 793

I am using ionic 3.15 and trying to use the permissions plugin. Using the code in the docs as mentioned here.

I have the code as shown in the docs but will mention it again here.

this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.CAMERA).then(
  success => console.log('Permission granted'),
  err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.CAMERA)
);

I always get hasPermission: false.

I do not get any prompt for turning the permissions on.

What is to be done here I am clueless.

Thanks.

2 Answers

You need to add something along the lines of:

this.androidPermissions.requestPermissions(
        [
          this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE,
          this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE
        ]
      );

You check to see if you have permissions, if it is false, then execute the above code to prompt the user to allow.

It seems like you have to make sure that you are using the permission you requested. Otherwise no prompt will be shown and the androidPermissions.requestPermissions will immediately return false.

From the docs:

Android 26 and above: due to Android 26's changes to permissions handling (permissions are requested at time of use rather than at runtime,) if your app does not include any functions (eg. other Ionic Native plugins) that utilize a particular permission, then requestPermission() and requestPermissions() will resolve immediately with no prompt shown to the user. Thus, you must include a function utilizing the feature you would like to use before requesting permission for it.

Related