
最新的Xcode更改了相册和相机的权限关键字,在进行真机调试,或真正在用户使用的时候需要添加这些权限,否则在程序正确时仍然会产生下面的错误:
Thread 4: signal SIGABRT
This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data.
SIGABRT
错误通常是由于应用程序在运行时遇到无法恢复的错误而崩溃。这种错误通常与以下几个原因有关:
AVCaptureSession 的配置不正确:可能是由于摄像头权限未被授予,或者设备不支持摄像头输入。为了排查这个问题,可以按照以下步骤来调试并修复:
检查摄像头权限
在使用摄像头之前,需要确保用户已经授予应用访问摄像头的权限。你可以在 Info.plist
文件中添加以下键,以请求摄像头和相册权限:
<key>NSPhotoLibraryUsageDescription</key>
<string>$请求访问相册</string>
<key>NSCameraUsageDescription</key>
<string>$请求使用摄像头</string>
但是我项目中没有找到 Info.plist文件啊,怎么办???
没事,我们让xcode给我们生成一个,点击项目,找到Info选项,在下面先选择一个Camera这个:

当你选中之后,就会在左侧看到Info.plist文件了:

然后在里面添加权限,添加权限有两种方式,一种是UI界面,你可以自己选择权限,一种是你手动黏贴选项进去,然后自动识别。
UI加权限
如果想手动选择加权限,也可以的,选中Info.plist右键,然后open as Property List:

就可以在列表中选中权限并添加了:

复制权限选项
在Info.plist右键打开,选择open as Source Code选项,就可以看到源代码模式:然后把刚才的权限访问黏贴上去,就可以了。然后从新运行app,就会提示你是否允许访问摄像头和相册权限。
