cordova android application permission
- Go to the official website to search for the plugin
https://cordova.apache.org/plugins/?q=cordova-plugin-android-permissions%20%20%20
- cmd add the plugin
cordova plugin add cordova-plugin-android-permissions
- Take the read and write permissions as an example, the judgment in the js file
var permissions = cordova.plugins.permissions;
// permissions.checkPermission(permission, successCallback, errorCallback);//校验权限
// permissions.requestPermission(permission, successCallback, errorCallback);//申请权限
// permissions.requestPermissions(permissions, successCallback, errorCallback);//申请权限集合
var permissions = cordova.plugins.permissions;
//校验app是否有安卓写入权限
permissions.checkPermission(permissions.WRITE_EXTERNAL_STORAGE, function (s) {
//hasPermission 验证是否成功
if (!s.hasPermission) {
//没有权限
//app申请写入权限
permissions.requestPermission(permissions.WRITE_EXTERNAL_STORAGE, function (s) {
if (s.hasPermission) {
//申请成功
alert("申请写入权限成功");
}
else {
//申请失败
alert("申请写入权限失败");
}
}, function (error) {
alert("请求写入权限失败");
});
} else {
//拥有权限
alert("已经拥有写入权限");
}
}, function (error) {
alert("请求写入权限失败");
});
- In the cordova project directory, you also need to add the following code to the android-plateform in config.xml
<config-file parent="/manifest" target="AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</config-file>