App permissions on Android Marshmallow in PhoneGap

余生颓废 提交于 2019-12-12 07:26:57

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/34117301/app-permissions-on-android-marshmallow-in-phonegap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!