The company’s Electron application, occasionally in
Equipment inspection 时 
CrashLater, after investigation, it was found that the current App does not have the permission of camera and microphone. Results in
Equipment inspectionThere was a crash.


For macOS 10.14 and above, developers must explicitly grant microphone and camera rights to their applications. Otherwise, the camera and microphone of the system cannot be invoked. For details, see: Requesting Authorization for Media Capture on macOS

According to the Apple document, if you want to use the microphone and camera permissions, you need to specify related attributes in the PList file, as follows:

  • The microphone: NSMicrophoneUsageDescription
  • Camera: NSCameraUsageDescription

These two properties you can see from the Description that’s a property that tells you why your program uses the microphone and camera.

In contrast, the Electron App packaging is generally carried out using the library electron-Builder, which has the following attribute in the documentation for MAC packaging: ExtendInfo, which is used to add your custom attributes to a plist file, is written in the electro builder. Yml file as follows:

MAC: extendInfo: NSMicrophoneUsageDescription: please allow access to your microphone NSCameraUsageDescription this program: please allow this application access to your cameraCopy the code

But when you do that, it doesn’t really do anything, because these two properties are just used to explain why your application is requesting permissions. But it does not specify the behavior of requesting permission.

To specify the permission to apply for cameras and microphones, the following attributes are required:

  • com.apple.security.device.camera
  • com.apple.security.device.audio-input

And these two attributes, when added, have a premise, that you must enable hardenedRuntime, this thing is to strengthen the integrity of the application Runtime, such as want to see details, see: Hardened Runtime Entitlements

Now let’s add hardenedRuntime:

mac:
  hardenedRuntime: trueExtendInfo: NSMicrophoneUsageDescription: please allow this application access to your microphone NSCameraUsageDescription: please allow this application access to your cameraCopy the code

HardenedRuntime, which defaults to true in 21.1.3 of the electron Builder, defaults to false in 21.1.2 to 20.41.0. Later versions do not have this property.

Then the behavior in the claim needs to utilize the Entitlements attribute. The code is as follows:

electron-builder.yml

mac:
  entitlements: entitlements.mac.plist
  hardenedRuntime: trueExtendInfo: NSMicrophoneUsageDescription: please allow this application access to your microphone NSCameraUsageDescription: please allow this application access to your cameraCopy the code

entitlements.mac.plist

<? xml version="1.0" encoding="UTF-8"? > <! DOCTYPE plist PUBLIC"- / / / / DTD PLIST Apple 1.0 / / EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>com.apple.security.device.audio-input</key>
    <true/>
    <key>com.apple.security.device.camera</key>
    <true/>
  </dict>
</plist>Copy the code

But when you try this, your app crashes when it opens, or doesn’t pack properly at all.

This is because when you turn on hardenedRuntime to enforce security in your application, you need to make it a little more secure. That is, you need to specify the following properties in Entitlements. Mac.plist:

  • com.apple.security.cs.allow-jit
  • com.apple.security.cs.allow-unsigned-executable-memory
  • com.apple.security.cs.allow-dyld-environment-variables

So now the final entitlements.mac.plist content is as follows:

<? xml version="1.0" encoding="UTF-8"? > <! DOCTYPE plist PUBLIC"- / / / / DTD PLIST Apple 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>Copy the code

From there, your Electron App should be able to request/use the camera and microphone properly on macOS.