cordova-plugin-android-permissions icon indicating copy to clipboard operation
cordova-plugin-android-permissions copied to clipboard

Check permission Always return success

Open asadwaheed1 opened this issue 8 years ago • 12 comments

I used checkPermission method but its always returning success even when permission is not granted. Here's my code:

CheckPermissions(data) { this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE).then( success => { console.log('Permission already granted'); this.playVideo(data); }, err => console.log('Cannot check for permission')); }

What am i missing?

asadwaheed1 avatar Aug 16 '17 11:08 asadwaheed1

You aren't checking the result in the success callback.

The success callback of the checkPermission() method returns an object that looks like: hasPermission: true or hasPermission: false

Try changing your code like this:

CheckPermissions(data) { this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE).then( success => { if (success.hasPermission){ console.log('Permission already granted'); this.playVideo(data); } else { console.log('Permission not already granted'); } }, err => console.log('Cannot check for permission')); }

You might also want to rename the 'success' variable to something like 'result' or 'data' to make the code a bit easier to read.

brentmitch avatar Aug 24 '17 21:08 brentmitch

Thanks for the info on the success object. But when I try this out on an Android 6.0.1 device I do not get any prompt when I use the .requestPermission method.

Any idea why is that?

prantikv avatar Aug 25 '17 06:08 prantikv

@brentmitch I already tried that hasPermission always returns success on android 6.0.1.

asadwaheed1 avatar Aug 25 '17 12:08 asadwaheed1

Permission popup does not even show

asadwaheed1 avatar Aug 25 '17 12:08 asadwaheed1

what is your target SDK in config.xml? if it is 23, then popup window, will never show up, but permissions will granted, and if you will change to 24 and up, the popup window should appear.

denys-glu avatar Aug 25 '17 12:08 denys-glu

@oggyman my target sdk version is 25 still no permission requesting popup shows up and it always returns success but when i check in settings permissions are not granted

asadwaheed1 avatar Sep 07 '17 07:09 asadwaheed1

Same problem here!

luchusnet avatar Sep 29 '17 12:09 luchusnet

Only the permissions that are listed in AndroidManifest.xml will be called.

marcelo-ribeiro avatar Oct 03 '17 19:10 marcelo-ribeiro

I get the same problem here. I always get the success callback, no popup is shown up on my test device (android 7.0). @marcelo-ribeiro all permissions are listed in AndroidManifest.xml too but it doesn't work.

[UPDATE] Currently, I have to ignore the 'checkPersmission' function and using 'requestPermissions' directly,

xuantruong2k avatar Oct 07 '17 09:10 xuantruong2k

@xuantruong2k use cordova diagnostic plugin to request and check permissions for both ios and android

asadwaheed1 avatar Oct 09 '17 10:10 asadwaheed1

Could you check which cordova-android version you are using? The build tools in earlier cordova-android version maybe too old. It might can let you build an app and run on new platform, but some functions of that app might have compatible issues.

If you are asking 'normal' permissions, it will always returns you success. https://developer.android.com/guide/topics/permissions/normal-permissions.html

At last, this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE seems should be this.androidPermissions.READ_EXTERNAL_STORAGE Could you help me to check that?

NeoLSN avatar Oct 18 '17 15:10 NeoLSN

I was able to get this to work with the following code (except, in my case, I was requesting permission to use the camera):

openCameraWithPermission = function(successCallback, failureCallback) { var action = this.openCamera; $ionicPlatform.ready(function() { var cordovaPermissions = cordova.plugins.permissions; var permission = cordovaPermissions.CAMERA; cordovaPermissions.checkPermission(permission, function(status) { if (status.hasPermission) { action(successCallback, failureCallback); } else { cordovaPermissions.requestPermission( permission, function(status) { if(status.hasPermission) { action(successCallback, failureCallback); } else { failureCallback('This app does not have permission to access the camera. Please enable camera access via your device settings'); } }, function() { failureCallback('This app does not have permission to access the camera. Please enable camera access via your device settings'); }); } }); }); };

I also needed to add the relevant permissions to platforms/android/AndroidManifest.xml.

CodeTectonics avatar Oct 30 '17 12:10 CodeTectonics