我正在使用带有以下插件的METEOR@1.4.2.3:
cordova:cordova-plugin-backbutton 0.3.0
cordova:cordova-plugin-camera 2.1.1
cordova:cordova-plugin-device 1.1.4
cordova:cordova-plugin-sim 1.3.2
cordova:cordova.plugins.diagnostic 2.3.16
cordova:phonegap-plugin-barcodescanner 3.0.0
我尝试为Android API级别26(从API级别22到26)实现新的正确结构,但它不起作用。
代码:
function onDeviceReady() {
console.log(device.cordova);
cordova.plugins.diagnostic.requestRuntimePermissions(function(statuses) {
for (var permission in statuses) {
switch (statuses[permission]) {
case cordova.plugins.diagnostic.permissionStatus.GRANTED:
console.log("Permission granted to use " + permission);
break;
case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED:
console.log("Permission to use " + permission + " has not been requested yet");
break;
case cordova.plugins.diagnostic.permissionStatus.DENIED:
console.log("Permission denied to use " + permission + " - ask again?");
break;
case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied to use " + permission + " - guess we won't be using it then!");
break;
}
}
}, function(error) {
console.error("The following error occurred: " + error);
}, [
cordova.plugins.diagnostic.permission.CAMERA,
cordova.plugins.diagnostic.permission.WRITE_EXTERNAL_STORAGE,
cordova.plugins.diagnostic.permission.READ_EXTERNAL_STORAGE,
cordova.plugins.diagnostic.permission.READ_PHONE_STATE
]);
}
我总是会收到此错误,并且APP被冻结:
I20180831-10:52:52.483(2)? 08-31 10:52:44.612 29275 29275 I铬:[INFO:CONSOLE(5283)]“未捕获的TypeError:无法读取未定义的属性'CAMERA'”,来源:http://localhost:12136/app/app.js?hash=4b27536f027d34508de1ae9fccb904c97e426df1(5283)
答案 0 :(得分:0)
欢迎堆栈溢出。
这样的错误消息
无法读取未定义的属性'CAMERA'
用来告诉您在代码中查找.CAMERA
,即以下行:
cordova.plugins.diagnostic.permission.CAMERA,
这意味着cordova.plugins.diagnostic.permission
未定义。这意味着已经定义了cordova.plugins.diagnostic
,因此您需要查看该插件的文档。
这将告诉您在哪里看,下一条建议是编写防御性代码以应对意外情况。像这样:
if (cordova && cordova.plugins && cordova.plugins.diagnostic && cordova.plugins.diagnostic.permission) {
// Everything is defined, expect cordova.plugins.diagnostic.permission.CAMERA to be valid
} else {
console.error("Something undefined in cordova.plugins.diagnostic.permission")
}
答案 1 :(得分:0)
我解决了这个问题。
我要添加插件
cordova:cordova-plugin-android-permissions 1.0.0
该项目。
修改后的函数OnDeviceReady:
function onDeviceReady() {
console.log(device.cordova);
var permissions = cordova.plugins.permissions;
permissions.checkPermission(permissions.READ_PHONE_STATE, function( status ){
if ( status.hasPermission ) {
console.log("Yes (READ_PHONE_STATE) ");
}
else {
console.warn("No (READ_PHONE_STATE) ");
permissions.requestPermission(permissions.READ_PHONE_STATE, success, error('READ_PHONE_STATE'));
}
});
permissions.checkPermission(permissions.CAMERA, function( status ){
if ( status.hasPermission ) {
console.log("Yes (CAMERA) ");
}
else {
console.warn("No (CAMERA) ");
permissions.requestPermission(permissions.CAMERA, success, error("CAMERA"));
}
});
permissions.checkPermission(permissions.WRITE_EXTERNAL_STORAGE, function( status ){
if ( status.hasPermission ) {
console.log("Yes (WRITE_EXTERNAL_STORAGE) ");
}
else {
console.warn("No (WRITE_EXTERNAL_STORAGE) ");
permissions.requestPermission(permissions.WRITE_EXTERNAL_STORAGE,
success,error("WRITE_EXTERNAL_STORAGE")
);
}
});
permissions.checkPermission(permissions.READ_EXTERNAL_STORAGE, function( status ){
if ( status.hasPermission ) {
console.log("Yes (READ_EXTERNAL_STORAGE) ");
}
else {
console.warn("No (READ_EXTERNAL_STORAGE) ");
permissions.requestPermission(permissions.READ_EXTERNAL_STORAGE, success, error('READ_EXTERNAL_STORAGE'));
}
});
function error(permission4request) {
console.warn(permission4request + ' permission is not turned on');
console.log(permission4request + ' permission is not turned on');
}
fdsf
function success( status ) {
if( !status.hasPermission ) error();
}
loginLogic();
}
此代码与API级别26配合使用以请求权限。