Vipul Aggarwal Vipul Aggarwal - 2 months ago 49x
Android Question

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.


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.


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 android@5.0.0
phonegap platform add android@5.0.0

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

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);

    if(status == "GRANTED"){
                if(status == "GRANTED"){
                    // Handle other cases
            }, function(error){
}, onError);