Electron Mac申请摄像头权限

  1. 在 package.json 文件中的 build 参数中的 mac 增加如下代码:
    {
      build: {
        "mac": {
          "icon": "build/icons/icon.icns",
          "entitlements": "entitlements.mac.plist",
          "hardenedRuntime": true,
          "extendInfo": {
            "NSMicrophoneUsageDescription": "请允许本程序访问您的麦克风",
            "NSCameraUsageDescription": "请允许本程序访问您的摄像头"
          }
        },
      }
    }
  2. 在跟目录新建 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>
  3. 在主线程窗口 on dom-ready 事件之后添加如下代码:
    import { systemPreferences } from 'electron'
    import { platform } from 'os'
    
     this.mainWindow.webContents.once('dom-ready', () => {
      if (platform().includes('darwin')) {
            let getMediaAccessStatus = systemPreferences.getMediaAccessStatus('microphone')
            if(getMediaAccessStatus !== 'granted'){
              //请求麦克风、摄像头权限
              systemPreferences.askForMediaAccess('microphone')
              systemPreferences.askForMediaAccess('camera')
            }
          }
    })

     

© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容