在运行时离子

时间:2016-03-29 10:49:44

标签: cordova ionic-framework android-6.0-marshmallow

在Android Marshmallow中,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时如何在离线时检查和授予运行时的Permissons?

2 个答案:

答案 0 :(得分:30)

您可以使用cordova-diagnostic-plugin检查并请求Android运行时权限:

检查权限:

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

答案 1 :(得分:0)

DaveAlden 解决方案是唯一对我有用的解决方案。请注意,此插件基于 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]

最后,自 Dave 的回答以来,诊断插件的 API 发生了变化,所以这是当前的工作代码:

要检查是否授予权限:

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