Electron 中查询和申请设备权限

Electron 中查询和申请设备权限

本文详细介绍了 Electron 项目中不同操作系统如何实现摄像头、麦克风、屏幕权限的查询和申请。

Electron 相关 API 介绍

js 复制代码
- mediaType: string
    - microphone      
    - camera          
    - screen          
- return: string 
    - not-determined
    - granted
    - denied
    - restricted
    - unkonwn
js 复制代码
- mediaType: string
    - microphone    
    - camera        
- return: Promise,传递一个无效的 mediaType 时 Promise 的状态也为 rejected。

注意: askForMediaAccess 方法的弹窗提醒只会出现一次,第一次出现拒绝授权后,以后必须要在系统首选项面板中更改权限后重新启动才能使权限生效。

不同操作系统 API 支持情况

macOS Windows Linux
getMediaAccessStatus
askForMediaAccess

权限查询、申请示例代码

js 复制代码
// main.js

const execCommond = require("child_process").exec;

/**
 * @param {String} device 'microphone' | 'camera'
 */
async function checkAndApplyDevicesAccessPrivilege(device) {
  if (process.platform === "linux") return;
  const promptText = {
    camera: "当前应用无摄像头权限,请授权后重新启动应用。",
    microphone: "当前应用无麦克风权限,请授权后重新启动应用。",
  };
  const devicePrivilege = systemPreferences.getMediaAccessStatus(device);
  if (devicePrivilege !== "granted") {
    if (process.platform === "darwin") {
      try {
        const isAuthorization = await systemPreferences.askForMediaAccess(device);
        if (!isAuthorization) {
          console.log(promptText[device]);
        }
      } catch (error) {
        console.log(promptText[device]);
      }
    } else {
      console.log(promptText[device]);
    }
  }
}


function checkAndApplyScreenShareAccessPrivilege() {
  if (process.platform === "linux") return;
  const screenPrivilege = systemPreferences.getMediaAccessStatus("screen");
  if (screenPrivilege !== "granted") {
    if (process.platform === "darwin") {
      console.log('当前应用无屏幕捕获权限,即将跳转至授权页面,请授权后重新启动应用。')
      execCommond(`open x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture`);
    }
}

使用案例:

js 复制代码
// main.js
...
app.whenReady().then(async () =>{
    createWindow()
    await checkAndApplyDevicesAccessPrivilege("camera");
    await checkAndApplyDevicesAccessPrivilege("microphone");
    checkAndApplyScreenShareAccessPrivilege();
})
...

其他问题

检测出无权限后想要弹窗提示怎么做?

通过 Electron 的主进程模块 dialog 可以实现系统级的弹窗提示,详细介绍见官网。

使用案例:

js 复制代码
// main.js
const { dialog } = require("electron");

dialog.showMessageBoxSync(browserWindow, {
        message:'提示消息',
        type: "warning",
});

检测出无权限后想跳转至权限设置窗口如何实现?

通过 Node.js 子进程 child_process 的 exec 模块执行命令行实现窗口跳转,详细介绍见官网。

使用案例:

js 复制代码
// main.js

const execCommond = require("child_process").exec;



/**
 * @param {String} device 'microphone' | 'camera'
 */
function jumpToPrivilegeSettings(device) {
  if ((device !== "camera" && device !== "microphone") || process.platform === "linux") return;
  const privacyTypeObjOfMac = {
    camera: "Privacy_Camera",
    microphone: "Privacy_Microphone",
  };
  const privacyTypeObjOfWindows = {
    camera: "privacy-webcam",
    microphone: "privacy-microphone",
  };
  if (process.platform === "darwin") {
    execCommond(`open x-apple.systempreferences:com.apple.preference.security?${privacyTypeObjOfMac[device]}`);
  } else {
    execCommond(`start ms-settings:${privacyTypeObjOfWindows[device]}`);
  }
}

... 
    // 检测到无麦克风权限后
   jumpToPrivilegeSettings('microphone')   
...
相关推荐
一拳不是超人10 小时前
Electron主窗口弹框被WebContentView遮挡?独立WebContentView弹框方案详解!
前端·javascript·electron
柯南95274 天前
Electron 无边框窗口拖拽实现
vue.js·electron
卸任4 天前
Windows判断是笔记本还是台式
前端·react.js·electron
一拳不是超人5 天前
Electron 实战全解析:基于 WebContentView 的多视图管理系统
前端·javascript·electron
lpfasd1239 天前
Tauri vs Electron:高质量Word/PDF导出效果深度对比
electron·pdf·word
卸任9 天前
Electron判断是内置摄像头还是接摄像头
前端·react.js·electron
贺今宵12 天前
Capacitor打包electron为apk
electron
一文解千机12 天前
wine 优化配置及显卡加速,完美运行Electron 编译的程序(新榜小豆芽、作家助手、小V猫等)
linux·ubuntu·electron·wine·wine优化配置·wine显卡加速·wine大型游戏
weixin_4255437317 天前
TREA CN 3.3.30 + GLM-5 王炸更新
ai·electron
一枚小太阳17 天前
想学 Electron?这份「能跑的示例集」一篇搞懂
前端·electron