macOSでカメラを使うアプリを作るとき、'com.apple.security.device.camera' entitlementエラーが出たら


macOSでカメラを使うアプリを作るとき、以下のようなエラーが出ることがある。

2020-08-03 22:26:56.800910+0900 Day9[16262:644033] Metal API Validation Enabled
2020-08-03 22:26:56.904507+0900 Day9[16262:644432] [access] This app has crashed because it has a hardened runtime and attempted to access privacy-sensitive data without an entitlement indicating its intent to access this data. The app must have the 'com.apple.security.device.camera' entitlement.
(lldb)

これは、カメラを使うアプリはセキュリティの制限があり、それをあらかじめ指示しなくてはならないからである。

  • Xcodeから、Cmd-1→infor.plist→右クリックしてAdd Row→Keyのところをダブルクリックして「NSCameraUsageDescription」と入力。自動的に「Privacy - Camera Usage Desciption」に変わる。
  • Valueをダブルクリックして任意のメッセージを入れる。「カメラからの画像を表示します。
  • Xcodeから、Cmd-1→MyApp→TARGETS→MyApp→Signing & Capabilities→All→Hardend Runtime→Cameraの左をcheck
  • Cmd-Rすると、「“MyApp.app”からカメラにアクセスしようとしています。」と、さきほどのメッセージと共に聞いてくるので「OK」

参考