在运行时请求权限Ionic

18
在Android Marshmallow中,当应用程序运行时用户会授予权限,而不是在安装应用程序时授予权限。因此,在Ionic中如何在运行时检查和授予权限?
2个回答

33
您可以使用 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
肯定的是,cordova.plugins.diagnostic.runtimePermission.CAMERA === cordova.plugins.diagnostic.runtimePermission['CAMERA'] - DaveAlden
不好意思,出现了一个 BUG。当我请求检查时,我必须使用 ['CAMERA'],但是当“请求权限”时,我必须使用 cordova.plugins.diagnostic.runtimePermission.CAMERA - user285594
我稍后会在问题跟踪器中发布。 - user285594
1
但在Javascript中,这两种形式在程序上是相同的:https://jsfiddle.net/dpa99c/k2460h8u/ - DaveAlden
1
@JamesN 这是一个本地插件,无法在Ionic View中使用,因为它是HTML5包装器。 若要调试,请从您的Cordova项目构建本地Android应用程序,然后将Chrome DevTools连接到设备上的远程Webview。 - DaveAlden
显示剩余6条评论

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 的 providers 列表中添加 Diagnostic:

import { Diagnostic } from '@ionic-native/diagnostic/ngx';
...
providers: [..., Diagnostic]

最后,自从戴夫的答案以来,诊断插件的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);
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接