dpa99c / cordova-diagnostic-plugin

Cordova/Phonegap plugin to manage device settings
539 stars 361 forks source link

Camera Requests Permission, but returns DENIED_ALWAYS #268

Closed munsterlander closed 6 years ago

munsterlander commented 6 years ago

So, the following code appears to work in that I get the popup but when I tap Allow, I get the DENIED_ALWAYS response. I have tried using the tryAnotherWay() function instead of the firstTry() function, but it fails as well. This is on Cordova 6.5.0 and Cordova-Android 6.2.3. I want to just use one function, but I have been trying all available functions to see what will work. Am I missing something?

function firstTry(){
 cordova.plugins.diagnostic.requestCameraAuthorization(
        function(status){
            if(status == cordova.plugins.diagnostic.permissionStatus.GRANTED)
                alert("Authorization request for camera use was granted.");
            else
                tryAgain();
        }, function(error){
            alert("The following error occurred: "+error);
        }, true
    );   
}

function tryAgain(){
        cordova.plugins.diagnostic.getPermissionsAuthorizationStatus(function(statuses){
    for (var permission in statuses){
        switch(statuses[permission]){
            case cordova.plugins.diagnostic.permissionStatus.GRANTED:
                alert("Permission granted to use "+permission);
                break;
            case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED:
                alert("Permission to use "+permission+" has not been requested yet");
                break;
            case cordova.plugins.diagnostic.permissionStatus.DENIED:
                alert("Permission denied to use "+permission+" - ask again?");
                break;
            case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
                alert("Permission permanently denied to use "+permission+" - guess we won't be using it then!");
                tryAnotherWay();
                break;
        }
    }
}, function(error){
    alert("The following error occurred: "+error);
},[
    cordova.plugins.diagnostic.permission.CAMERA
]);
}

function tryAnotherWay(){
    cordova.plugins.diagnostic.requestRuntimePermission(function(status){
    switch(status){
        case cordova.plugins.diagnostic.permissionStatus.GRANTED:
            alert("Permission granted to use the camera");
            break;
        case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED:
            alert("Permission to use the camera has not been requested yet");
            break;
        case cordova.plugins.diagnostic.permissionStatus.DENIED:
            alert("Permission denied to use the camera - ask again?");
            break;
        case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
            alert("Permission permanently denied to use the camera - guess we won't be using it then - Forever!");
            break;
    }
}, function(error){
    alert("The following error occurred: "+error);
}, cordova.plugins.diagnostic.permission.CAMERA);
}
dpa99c commented 6 years ago

Did you (as documented) include the necessary <uses-permission> keys in the AndroidManifest.xml? In the case of requestCameraAuthorization(), the relevant keys would be CAMERA and READ_EXTERNAL_STORAGE.

Also see the various example project for a working illustration of how to use this plugin.

munsterlander commented 6 years ago

Yes. I am using Monaca to build and per their documentation, they use this format:

<preference name="android-manifest/uses-permission/[@android:name='android.permission.CAMERA']/@android:name" delete="true" />
<preference name="android-manifest/uses-permission/[@android:name='android.permission.READ_EXTERNAL_STORAGE']/@android:name" delete="true" />

I get the prompt, I click allow and then denied.

Edit: So clearly something is going on during my build, because the example APK works fine. I will go back to Monaca to figure out what is happening with their config.

Edit2: So decompiling the APK shows the camera permission is not getting added which is resulting in the failure. Thanks for linking to the example app. This is closed.