在AndroidMarshmallow的应用程序权限在PhoneGap [英] App permissions on Android Marshmallow in PhoneGap

查看:483
本文介绍了在AndroidMarshmallow的应用程序权限在PhoneGap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android Marshmallow中,我们需要在Apps中单独授予位置,文件等权限。
在PhoneGap应用程序中有任何瑕疵,我可以检查是否有权限可用和提示用户提供权限。

解决方案

UPDATE 16/02/2016



Phonegap立即建立支持API 23 - hoooray!



或者如果您在本地构建,只需Cordova / Phonegap CLI 6+


$ b

ORIGINAL ANSWER



目前,这当然可以使用Cordova / Phonegap CLI,但 Phonegap Build 尚不支持API 23(Android 6.0 / Marshmallow)。



首先,您需要使用Android平台的v5.0.0(或更高版本),它使用API​​ 23.



截至今天,默认版本是4.1.1(使用API​​ 22),因此您需要在添加平台时明确指定版本:

  cordova平台添加android@5.0.0 
phonegap平台添加android@5.0.0

核心插件本身正在升级以支持请求适当的Android 6运行时权限。所以你需要直接从GitHub repos的master分支安装出血边缘版本,而不是通过npm插件注册表。例如,使用:

  cordova插件添加https://github.com/apache/cordova-plugin-file $ b $ 

p>不是npm版本:

  cordova插件add cordova-plugin-file 

这将得到你v3.0.0



请注意,主分支上的版本不发布,因此可能包含错误。



所以替代方案(我已选择),是继续使用发布版本的插件支持请求Android运行时权限),但使用 cordova-diagnostic-plugin 请求Android运行时权限,相应的核心插件需要在尝试使用核心插件API之前。



例如,要使用位置插件,请通过npm继续使用发行版本:

  cordova插件add cordova-plugin-geolocation 

确保已使用诊断插件授予运行时权限:

 函数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 {
//处理其他情况
}
},函数(错误){
console.error(error);
});
}
},onError);


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.

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

这篇关于在AndroidMarshmallow的应用程序权限在PhoneGap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆