Electron-EE框架 在 MacOS 下 申请摄像头及麦克风权限踩坑指南

平台:mac
在项目打包后,访问某一页面,调用了摄像头麦克风,devTool disconnected from the page ...,然后就白屏,想到可能是权限的问题
步骤如下:

  1. 在项目根目录下创建一个entitlements.mac.plist文件,并写入下面的代码;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>com.apple.security.cs.allow-jit</key>
    <true/>
    <key>com.apple.security.cs.allow-unsigned-executable-memory</key>
    <true/>
    <key>com.apple.security.cs.allow-dyld-environment-variables</key>
    <true/>
    <key>com.apple.security.device.audio-input</key>
    <true/>
    <key>com.apple.security.device.camera</key>
    <true/>
  </dict>
</plist>
  1. 在package.json中配置plist。
"entitlements": "entitlements.mac.plist",
"hardenedRuntime": true,
"extendInfo": {
    "NSMicrophoneUsageDescription": "请允许本程序访问您的麦克风",
    "NSCameraUsageDescription": "请允许本程序访问您的摄像头"
},

image.png

hardenedRuntime 这个属性,在 electron-builder 的 21.1.3 版本已经默认为 true ,而在 21.1.2 ~ 20.41.0 版本里,这个属性的默认值是 false。再往后的版本里没有这个属性。

打包之后在某些电脑出现白屏devTool disconnected from the page ...,后来查询得知

在 macOS 10.13 High Sierra 或更低版本上不需要用户授权,因此此方法总是返回 granted。 macOS 10.14 Mojave 或更高版本需要授权访问 麦克风 和 摄像头。 macOS 10.15 Catalina 或更高版本需要授权访问 屏幕。原文

高版本必须主动去询问

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容