cordova-plugin-android-permissions
cordova-plugin-android-permissions copied to clipboard
Check permission Always return success
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?
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.
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?
@brentmitch I already tried that hasPermission always returns success on android 6.0.1.
Permission popup does not even show
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.
@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
Same problem here!
Only the permissions that are listed in AndroidManifest.xml will be called.
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 use cordova diagnostic plugin to request and check permissions for both ios and android
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?
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.