How to check camera permission which would be supported across all browsers?

余生长醉 提交于 2021-01-28 07:10:57

问题


I am using the following code for checking if my react application has a camera permission:-

  checkForCameraPermission = () => {
    try {
      navigator.permissions.query({ name: 'camera' }).then(permissionStatus => {
        // granted, denied, prompt
        switch (permissionStatus.state) {
          case 'denied':
            // eslint-disable-next-line no-alert
            // alert(
            //   'You need to provide camera permission and reload page to continue futher with KYC journey or else please download the EarlySalary App to continue further.'
            // );
            this.setState({
              cameraDialogStatus: true
            });

            break;
          default:
            break;
        }
        // eslint-disable-next-line no-param-reassign
        permissionStatus.onchange = () => {
          console.log(`Permission changed to ${this.state}`);
        };
      });
    } catch (error) {
      console.log('camera error', error);
      // alert('TEST');
      this.setState({ isShowTestDialog: true });
    }
  };

I am using this in component did mount lifecycle method it works fine on most of the browsers but on some browsers it is not supported. So I tried finding out why it wasn't working on some devices and I finally got my answer with the MDN web docs at the following link:- [https://developer.mozilla.org/en-US/docs/Web/API/Navigator/permissions][1]

Is there a better approach that I can take as my react application is being used on Desktops as well as on mobile phone's and I would like the application to work as expected on all the devices and if not show why it is failing?

Any help or suggestion is much appreciated. Thank you for your time and support.

来源:https://stackoverflow.com/questions/64679982/how-to-check-camera-permission-which-would-be-supported-across-all-browsers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!