Electron - Macos 麦克风和摄像头权限申请的坑

1.electron plist权限配置可以参考 链接
2.参考 electron 文档,askForMediaAccess直到macOS 10.14 Mojave才需要用户同意,因此如果您的系统运行的是10.13 High Sierra或更低版本,此方法将始终返回“true”。

/**
     * A promise that resolves with `true` if consent was granted and `false` if it was
     * denied. If an invalid `mediaType` is passed, the promise will be rejected. If an
     * access request was denied and later is changed through the System Preferences
     * pane, a restart of the app will be required for the new permissions to take
     * effect. If access has already been requested and denied, it _must_ be changed
     * through the preference pane; an alert will not pop up and the promise will
     * resolve with the existing access status.
     *
     * **Important:** In order to properly leverage this API, you must set the
     * `NSMicrophoneUsageDescription` and `NSCameraUsageDescription` strings in your
     * app's `Info.plist` file. The values for these keys will be used to populate the
     * permission dialogs so that the user will be properly informed as to the purpose
     * of the permission request. See Electron Application Distribution for more
     * information about how to set these in the context of Electron.
     *
     * This user consent was not required until macOS 10.14 Mojave, so this method will
     * always return `true` if your system is running 10.13 High Sierra or lower.
     *
     * @platform darwin
     */
    askForMediaAccess(mediaType: 'microphone' | 'camera'): Promise<boolean>;

为了适配,以下是解决方案:

async checkAndApplyDeviceAccessPrivilege(mediaType) {
     const privilege = systemPreferences.getMediaAccessStatus(mediaType);
     if (privilege !== 'granted') {
       if (mediaType === 'camera' ||  mediaType === 'microphone') {
         await systemPreferences.askForMediaAccess(mediaType);
         return systemPreferences.getMediaAccessStatus(mediaType);
       } else {
          // screen
       }
     }
     return privilege;
 }

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

相关阅读更多精彩内容

友情链接更多精彩内容