Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App permissions on Android Marshmallow in PhoneGap

In Android Marshmallow we need to give access to Location, File etc separately in Apps. Is there any wan in PhoneGap app that I can check is permissions are available or not and prompt the user to provide the permissions.

like image 391
Vipul Aggarwal Avatar asked Dec 06 '15 12:12

Vipul Aggarwal


2 Answers

UPDATE 16/02/2016

Phonegap Build now supports API 23 - hoooray!

Or if you build locally, just Cordova/Phonegap CLI 6+ and you'll get cordova-android@5+ platform by default.

ORIGINAL ANSWER

Currently, this is certainly possible using the Cordova/Phonegap CLI, but Phonegap Build doesn't yet support API 23 (Android 6.0 / Marshmallow).

Firstly, you need to use v5.0.0 (or above) of the Android platform, which uses API 23.

As of today, the default version is 4.1.1 (which uses API 22), so you need to explicitly specify the version when adding platform:

cordova platform add [email protected]
phonegap platform add [email protected]

The core plugins themselves are in the process of being upgraded to support requesting of appropriate Android 6 runtime permissions. So you'll need to install the "bleeding edge" versions directly from the master branch of the GitHub repos, as opposed to via the npm plugin registry. For example, use:

cordova plugin add https://github.com/apache/cordova-plugin-file

which should get you version 4.0.1-dev.

Not the npm release version:

cordova plugin add cordova-plugin-file

which will get you v3.0.0

Note that the versions on the master branches are not releases, so may contain bugs.

So the alternative (which I have opted for), is to continue using the release versions of the plugins (which don't yet support requesting of Android runtime permissions), but use cordova-diagnostic-plugin to request the Android runtime permissions that the corresponding core plugin requires before attempting to use the core plugin API.

For example, to use the Location plugin, continue to use the release version via npm:

cordova plugin add cordova-plugin-geolocation

But before requesting a location, ensure that runtime permission has been granted using the diagnostic plugin:

function requestLocation(){
    navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
}

cordova.plugins.diagnostic.getLocationAuthorizationStatus(function(status){
    if(status == "GRANTED"){
        requestLocation();
    }else{
        cordova.plugins.diagnostic.requestLocationAuthorization(function(status){
                if(status == "GRANTED"){
                    requestLocation();  
                }else{
                    // Handle other cases
                }
            }, function(error){
                console.error(error);
        });
    }
}, onError);
like image 58
DaveAlden Avatar answered Nov 15 '22 21:11

DaveAlden


It's possible to explicitly ask the user to enable a specific permission with following Cordova plugin: https://www.npmjs.com/package/cordova-plugin-android-permissions

Especially for older plugins this can be very useful.

like image 44
agassner Avatar answered Nov 15 '22 20:11

agassner