Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ionic/Angular Diagnostic.requestRuntimePermission如何?(文件不完整)_Javascript_Android_Angularjs_Cordova_Ionic Framework - Fatal编程技术网

Javascript Ionic/Angular Diagnostic.requestRuntimePermission如何?(文件不完整)

Javascript Ionic/Angular Diagnostic.requestRuntimePermission如何?(文件不完整),javascript,android,angularjs,cordova,ionic-framework,Javascript,Android,Angularjs,Cordova,Ionic Framework,在我的应用程序中,我需要申请相机、音频输入(Micro)和存储的许可。 由于Android 6,我想,您必须在运行时请求这些权限才能获得访问权限。到现在为止,一直都还不错。 要做到这一点,您需要可以找到的Ionic 2 Cordova插件“Diagnostics” 正如您所见,此文档非常不完整。它仅显示如何获取蓝牙的当前状态。 那你怎么称呼许可呢?为了使用开关/机箱,您恢复了什么状态。所有这些信息都是需要的,但不是这里 就我所尝试的而言,我能够发现存在某种diagnostic.requestRu

在我的应用程序中,我需要申请相机、音频输入(Micro)和存储的许可。 由于Android 6,我想,您必须在运行时请求这些权限才能获得访问权限。到现在为止,一直都还不错。 要做到这一点,您需要可以找到的Ionic 2 Cordova插件“Diagnostics”

正如您所见,此文档非常不完整。它仅显示如何获取蓝牙的当前状态。 那你怎么称呼许可呢?为了使用开关/机箱,您恢复了什么状态。所有这些信息都是需要的,但不是这里

就我所尝试的而言,我能够发现存在某种diagnostic.requestRuntimePermission函数。使用此功能,您可以执行以下操作:

           that.diagnostic.requestRuntimePermission('CAMERA')
             .then((status) =>{
                  switch(status){
                      case "GRANTED":
                          console.log("Permission granted to use the camera");
                          break;
                      case "DENIED":
                          console.log("Permission denied to use the camera - ask again?");
                          break;
                      case "DENIED_ALWAYS":
                          console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
                          break;
                  }
              }).catch(e => console.error(e));
 switch(status){
     case "GRANTED":
        console.log("Permission granted to use the camera");
        break;
     case "DENIED":
        //Here should I now call the PermissionRequest function
        console.log("Permission denied to use the camera - ask again?");
        break;
     case "DENIED_ALWAYS":
        console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
        break;
   }
this.diagnostic.permissionStatus
注意:这只是一个代码,我想它可能看起来像,但我不知道它是否有效。 因此,让我们以摄像头权限为例来解决这个问题: 要检查摄像头是否可用,请执行以下操作:

let successCallback = (isAvailable) => { console.log('Is available? ' + isAvailable); };
let errorCallback = (e) => console.error(e);
that.diagnostic.isCameraAvailable().then(successCallback).catch(errorCallback);
在那之后,我必须得到相机的授权,以知道许可是否被授予、拒绝或甚至没有被请求。这项工作如下:

 that.diagnostic.getCameraAuthorizationStatus()
 .then((state) => {
   switch(state){
       /*HERE should I work with the State in order to request permission or not*/
   }
}).catch(e => console.error(e));
在这一步上,我不知道该做什么了,因为我不知道我回到这里的状态是什么。 我认为是这样的:

           that.diagnostic.requestRuntimePermission('CAMERA')
             .then((status) =>{
                  switch(status){
                      case "GRANTED":
                          console.log("Permission granted to use the camera");
                          break;
                      case "DENIED":
                          console.log("Permission denied to use the camera - ask again?");
                          break;
                      case "DENIED_ALWAYS":
                          console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
                          break;
                  }
              }).catch(e => console.error(e));
 switch(status){
     case "GRANTED":
        console.log("Permission granted to use the camera");
        break;
     case "DENIED":
        //Here should I now call the PermissionRequest function
        console.log("Permission denied to use the camera - ask again?");
        break;
     case "DENIED_ALWAYS":
        console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
        break;
   }
this.diagnostic.permissionStatus
这不管用,但我想一定是这样的。 请帮帮我,告诉我:

  • 在此处返回哪个状态以及如何访问它

  • 如何使用diagnostics.requestRuntimePermission权限


是到原始Ionic2论坛的链接,带有相同的问题。

我认为您使用错误的cordova插件请求运行时权限。diganostics插件旨在告诉您某些设备功能(如照相机)是否可用。我建议在运行时请求权限。请注意,该插件还有一个可用的

编辑: 回答您的确切问题:根据此插件的说明,权限状态请求的响应将是以下对象属性之一:

permissionStatus: {
  GRANTED: string;
  DENIED: string;
  NOT_REQUESTED: string;
  DENIED_ALWAYS: string;
  RESTRICTED: string;
  GRANTED_WHEN_IN_USE: string;
};
在Ionic类中,您可以通过以下方式访问它,假设您通过以下方式在构造函数中注入了Ionic本机类:

constructor(private diagnostic: Diagnostic){}
您可以如下方式访问permissionStatus对象:

           that.diagnostic.requestRuntimePermission('CAMERA')
             .then((status) =>{
                  switch(status){
                      case "GRANTED":
                          console.log("Permission granted to use the camera");
                          break;
                      case "DENIED":
                          console.log("Permission denied to use the camera - ask again?");
                          break;
                      case "DENIED_ALWAYS":
                          console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
                          break;
                  }
              }).catch(e => console.error(e));
 switch(status){
     case "GRANTED":
        console.log("Permission granted to use the camera");
        break;
     case "DENIED":
        //Here should I now call the PermissionRequest function
        console.log("Permission denied to use the camera - ask again?");
        break;
     case "DENIED_ALWAYS":
        console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
        break;
   }
this.diagnostic.permissionStatus
随后,您可以重写switch case语句:

let permissionStatus = this.diagnostic.permissionStatus;
that.diagnostic.getCameraAuthorizationStatus()
  .then((state) => {
    switch(state){
      case permissionStatus.GRANTED:
        console.log("Permission granted to use the camera");
        break;
      case permissionStatus.DENIED:
        //Here should I now call the PermissionRequest function
        console.log("Permission denied to use the camera - ask again?");
        break;
      case permissionStatus.DENIED_ALWAYS:
        console.log("Permission permanently denied to use the camera - guess 
        we won't be using it then!");
        break;
    }
}).catch(e => console.error(e));
以及在运行时请求(例如)摄像机许可:

let permission = this.diagnostic.permission;
this.diagnostic.requestRuntimePermission(permission.CAMERA).then(
  success => {
    console.log('reuqestCameraAuthroization, success', success);
  },
  error => {
    console.log('reuqestCameraAuthroization, error', error);
  },
);
但我认为,在permissionStatus.DENIED或permissionStatus.NOT_请求的情况下,您不需要手动请求运行时权限,因为如上所述,如果不使用“false”参数调用getCameraAuthorizationStatus,则默认情况下,getCameraAuthorizationStatus将请求运行时权限


如果您不确定承诺的结果看起来如何,您可以将其记录到控制台(例如console.log(success))。您可以随时记录可注入(在您的案例中为this.diagnostic)以查看哪些公共方法和对象可用。

我认为您使用了错误的cordova插件来请求运行时权限。diganostics插件旨在告诉您某些设备功能(如照相机)是否可用。我建议在运行时请求权限。请注意,该插件还有一个可用的

编辑: 回答您的确切问题:根据此插件的说明,权限状态请求的响应将是以下对象属性之一:

permissionStatus: {
  GRANTED: string;
  DENIED: string;
  NOT_REQUESTED: string;
  DENIED_ALWAYS: string;
  RESTRICTED: string;
  GRANTED_WHEN_IN_USE: string;
};
在Ionic类中,您可以通过以下方式访问它,假设您通过以下方式在构造函数中注入了Ionic本机类:

constructor(private diagnostic: Diagnostic){}
您可以如下方式访问permissionStatus对象:

           that.diagnostic.requestRuntimePermission('CAMERA')
             .then((status) =>{
                  switch(status){
                      case "GRANTED":
                          console.log("Permission granted to use the camera");
                          break;
                      case "DENIED":
                          console.log("Permission denied to use the camera - ask again?");
                          break;
                      case "DENIED_ALWAYS":
                          console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
                          break;
                  }
              }).catch(e => console.error(e));
 switch(status){
     case "GRANTED":
        console.log("Permission granted to use the camera");
        break;
     case "DENIED":
        //Here should I now call the PermissionRequest function
        console.log("Permission denied to use the camera - ask again?");
        break;
     case "DENIED_ALWAYS":
        console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
        break;
   }
this.diagnostic.permissionStatus
随后,您可以重写switch case语句:

let permissionStatus = this.diagnostic.permissionStatus;
that.diagnostic.getCameraAuthorizationStatus()
  .then((state) => {
    switch(state){
      case permissionStatus.GRANTED:
        console.log("Permission granted to use the camera");
        break;
      case permissionStatus.DENIED:
        //Here should I now call the PermissionRequest function
        console.log("Permission denied to use the camera - ask again?");
        break;
      case permissionStatus.DENIED_ALWAYS:
        console.log("Permission permanently denied to use the camera - guess 
        we won't be using it then!");
        break;
    }
}).catch(e => console.error(e));
以及在运行时请求(例如)摄像机许可:

let permission = this.diagnostic.permission;
this.diagnostic.requestRuntimePermission(permission.CAMERA).then(
  success => {
    console.log('reuqestCameraAuthroization, success', success);
  },
  error => {
    console.log('reuqestCameraAuthroization, error', error);
  },
);
但我认为,在permissionStatus.DENIED或permissionStatus.NOT_请求的情况下,您不需要手动请求运行时权限,因为如上所述,如果不使用“false”参数调用getCameraAuthorizationStatus,则默认情况下,getCameraAuthorizationStatus将请求运行时权限


如果您不确定承诺的结果看起来如何,您可以将其记录到控制台(例如console.log(success))。您可以随时记录可注入(在您的案例中为this.diagnostic)以查看哪些公共方法和对象可用。

问题是,我使用的cordova插件也支持iOS,这对我来说非常重要。正如我提到的,它还提供了请求预任务功能。它们只是没有记录在案。但您可以通过这些功能获得访问权限。所以你的解决方案只适用于Android,但我也需要iOS。我更新了我的答案,希望它现在能进一步帮助你!正是我需要的!非常感谢。问题是,我使用的cordova插件也支持iOS,这对我来说是Nessary。正如我提到的,它还提供了请求预任务功能。它们只是没有记录在案。但您可以通过这些功能获得访问权限。所以你的解决方案只适用于Android,但我也需要iOS。我更新了我的答案,希望它现在能进一步帮助你!正是我需要的!非常感谢。