Cordova 正在运行时请求权限
在Android Marshmallow中,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时,因此如何在ionic中的运行时检查和授予权限?您可以使用检查和请求Android运行时权限: 检查权限:Cordova 正在运行时请求权限,cordova,ionic-framework,android-6.0-marshmallow,Cordova,Ionic Framework,Android 6.0 Marshmallow,在Android Marshmallow中,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时,因此如何在ionic中的运行时检查和授予权限?您可以使用检查和请求Android运行时权限: 检查权限: cordova.plugins.diagnostic.getPermissionAuthorizationStatus(function(status){ switch(status){ case cordova.plugins.diagnostic.runti
cordova.plugins.diagnostic.getPermissionAuthorizationStatus(function(status){
switch(status){
case cordova.plugins.diagnostic.runtimePermissionStatus.GRANTED:
console.log("Permission granted to use the camera");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.NOT_REQUESTED:
console.log("Permission to use the camera has not been requested yet");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED:
console.log("Permission denied to use the camera - ask again?");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
break;
}
}, function(error){
console.error("The following error occurred: "+error);
}, cordova.plugins.diagnostic.runtimePermission.CAMERA);
cordova.plugins.diagnostic.requestRuntimePermission(function(status){
switch(status){
case cordova.plugins.diagnostic.runtimePermissionStatus.GRANTED:
console.log("Permission granted to use the camera");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.NOT_REQUESTED:
console.log("Permission to use the camera has not been requested yet");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED:
console.log("Permission denied to use the camera - ask again?");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
break;
}
}, function(error){
console.error("The following error occurred: "+error);
}, cordova.plugins.diagnostic.runtimePermission.CAMERA);
请求许可:
cordova.plugins.diagnostic.getPermissionAuthorizationStatus(function(status){
switch(status){
case cordova.plugins.diagnostic.runtimePermissionStatus.GRANTED:
console.log("Permission granted to use the camera");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.NOT_REQUESTED:
console.log("Permission to use the camera has not been requested yet");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED:
console.log("Permission denied to use the camera - ask again?");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
break;
}
}, function(error){
console.error("The following error occurred: "+error);
}, cordova.plugins.diagnostic.runtimePermission.CAMERA);
cordova.plugins.diagnostic.requestRuntimePermission(function(status){
switch(status){
case cordova.plugins.diagnostic.runtimePermissionStatus.GRANTED:
console.log("Permission granted to use the camera");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.NOT_REQUESTED:
console.log("Permission to use the camera has not been requested yet");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED:
console.log("Permission denied to use the camera - ask again?");
break;
case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
break;
}
}, function(error){
console.error("The following error occurred: "+error);
}, cordova.plugins.diagnostic.runtimePermission.CAMERA);
解决方案是唯一对我有效的方法。请注意,此插件基于AndroidX,因此您必须安装以下文件:
ionic cordova plugin add cordova-plugin-androidx
ionic cordova plugin add cordova-plugin-androidx-adapter
然后您可以安装诊断插件文件:
ionic cordova plugin add cordova.plugins.diagnostic
npm install @ionic-native/diagnostic
还要记住将诊断添加到app.module上的提供者列表中:
import { Diagnostic } from '@ionic-native/diagnostic/ngx';
...
providers: [..., Diagnostic]
最后,诊断插件的API在Dave的回答后发生了变化,因此这是当前的工作代码:
检查是否授予权限:
this.diagnostic.getPermissionAuthorizationStatus(this.diagnostic.permission.CAMERA)
.then((status) => {
switch(status){
case this.diagnostic.permissionStatus.GRANTED:
console.log("Permission granted to use the camera");
break;
case this.diagnostic.permissionStatus.NOT_REQUESTED:
console.log("Permission to use the camera has not been requested yet");
break;
case this.diagnostic.permissionStatus.DENIED:
console.log("Permission denied to use the camera - ask again?");
break;
case this.diagnostic.permissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
break;
}
})
.catch((error) => {
console.error("The following error occurred: "+error);
});
this.diagnostic.requestRuntimePermission(this.diagnostic.permission.CAMERA)
.then((status) => {
switch(status){
case this.diagnostic.permissionStatus.GRANTED:
console.log("Permission granted to use the camera");
break;
case this.diagnostic.permissionStatus.NOT_REQUESTED:
console.log("Permission to use the camera has not been requested yet");
break;
case this.diagnostic.permissionStatus.DENIED:
console.log("Permission denied to use the camera - ask again?");
break;
case this.diagnostic.permissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
break;
}
})
.catch((error) => {
console.error("The following error occurred: "+error);
});
请求权限:
this.diagnostic.getPermissionAuthorizationStatus(this.diagnostic.permission.CAMERA)
.then((status) => {
switch(status){
case this.diagnostic.permissionStatus.GRANTED:
console.log("Permission granted to use the camera");
break;
case this.diagnostic.permissionStatus.NOT_REQUESTED:
console.log("Permission to use the camera has not been requested yet");
break;
case this.diagnostic.permissionStatus.DENIED:
console.log("Permission denied to use the camera - ask again?");
break;
case this.diagnostic.permissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
break;
}
})
.catch((error) => {
console.error("The following error occurred: "+error);
});
this.diagnostic.requestRuntimePermission(this.diagnostic.permission.CAMERA)
.then((status) => {
switch(status){
case this.diagnostic.permissionStatus.GRANTED:
console.log("Permission granted to use the camera");
break;
case this.diagnostic.permissionStatus.NOT_REQUESTED:
console.log("Permission to use the camera has not been requested yet");
break;
case this.diagnostic.permissionStatus.DENIED:
console.log("Permission denied to use the camera - ask again?");
break;
case this.diagnostic.permissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
break;
}
})
.catch((error) => {
console.error("The following error occurred: "+error);
});
嗨,Dave——这里的成功回调只需要一个状态参数。如果我想对成功采取其他措施,比如保存文件,该怎么办?在回调函数中似乎是一个自然的地方,因为您希望在保存之前等待权限得到确认,但这不是一个选项。我应该将状态存储在全局参数(YK)中吗?有更好的方法吗?Json数组
['CAMERA']
而不是cordova.plugins.diagnostic.runtimePermission.CAMERA
,否则它会失败。???当然cordova.plugins.diagnostic.runtimePermission.CAMERA===cordova.plugins.diagnostic.runtimePermission['CAMERA']
但在Javascript中,这两种形式在编程上是完全相同的:@JamesN它是一个本机插件,不能与作为HTML5包装器的Ionic View一起工作。要进行调试,请从Cordova项目构建本机Android应用程序,然后将Chrome DevTools连接到设备上的远程Webview。