Electron - Macos 麥克風(fēng)和攝像頭權(quán)限申請(qǐng)的坑

1.electron plist權(quán)限配置可以參考 鏈接。
2.參考 electron 文檔,askForMediaAccess直到macOS 10.14 Mojave才需要用戶同意,因此如果您的系統(tǒng)運(yùn)行的是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')
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容