Skip to content

Commit

Permalink
add macOS microphone and camera permission request
Browse files Browse the repository at this point in the history
  • Loading branch information
lony2003 committed Jul 7, 2024
1 parent 976ff40 commit 85236e3
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
8 changes: 7 additions & 1 deletion electron-builder.json5
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
]
}
],
"artifactName": "${productName}-Mac-${arch}-${version}-Installer.${ext}"
"artifactName": "${productName}-Mac-${arch}-${version}-Installer.${ext}",
"hardenedRuntime": true,
"entitlements": "entitlements.mac.plist",
"extendInfo": {
"NSMicrophoneUsageDescription": "Microphone",
"NSCameraUsageDescription": "Camera"
}
},
"win": {
"icon": "public/icon.ico",
Expand Down
63 changes: 63 additions & 0 deletions electron/macPermission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {systemPreferences} from 'electron'

enum EMediaType {
microphone = 'microphone', // 麦克风
camera = 'camera', // 相机
}

/**
* 访问状态
* 'not-determined':[未确定]表示用户尚未做出决定,或者系统尚未提示用户进行授权。
* 'granted':[已授权]表示用户已经明确授予了应用的权限。
* 'denied':[拒绝]表示用户拒绝了应用授权的请求。
* 'restricted':[受限]在某些情况下,可能是由于系统策略或其他安全限制导致应用无法获得改权限。
* 'unknown':[未知]在无法确定权限状态的情况下返回,可能是因为某种错误或其他不可预知的情况。
*/
type IAccessStatus = 'not-determined' | 'granted' | 'denied' | 'restricted' | 'unknown'

/**
* 请求媒体权限
* @param mediaType
*/
const requestMediaAccess = async (mediaType: EMediaType): Promise<IAccessStatus> => {

try {
// 获取当前媒体设备(在这里指麦克风或摄像头)的访问权限状态
const privilege: IAccessStatus = systemPreferences.getMediaAccessStatus(mediaType)

if (privilege !== 'granted') {
// 未授权,则重新唤起系统弹框,等待用户点击授权
await systemPreferences.askForMediaAccess(mediaType)
// 请求权限后,再次获取媒体访问状态并返回
return systemPreferences.getMediaAccessStatus(mediaType)
}
// 已授权,则直接返回媒体访问状态
return privilege
} catch (e) {
console.error('Failed to request media access:', e)
return 'unknown'
}

}

const requestAllMediaAccess = async ()=> {

while (true) {
let microphoneResult = await requestMediaAccess(EMediaType.microphone);

if (microphoneResult == 'granted') {
break;
}
}

while (true) {
let cameraResult = await requestMediaAccess(EMediaType.camera);

if (cameraResult == 'granted') {
break;
}
}

}

export default requestAllMediaAccess;
6 changes: 6 additions & 0 deletions electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as urlUtil from "node:url"
import {autoUpdater, UpdateInfo} from "electron-updater"
import * as url from "node:url"
import packageJson from "../package.json";
import requestAllMediaAccess from "./macPermission";

// The built directory structure
//
Expand Down Expand Up @@ -69,6 +70,11 @@ function createWindow() {

nativeTheme.themeSource = theme

//request camera and microphone permission(only for macOS)
if (process.platform === 'darwin') {
requestAllMediaAccess();
}

// win = new BrowserWindow({
// icon: path.join(process.env.VITE_PUBLIC, 'electron-vite.svg'),
// webPreferences: {
Expand Down
16 changes: 16 additions & 0 deletions entitlements.mac.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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>

0 comments on commit 85236e3

Please sign in to comment.