如果 info.plist 不包含某个键,如何避免应用程序崩溃?

How to avoid app crash if info.plist doesn't contain certain key?

我正在构建一个 SDK,在其中,我需要用户许可才能录制音频,为此用户应该在 info.plist 中添加 Privacy - Microphone Usage Description。我想确保应用程序不会崩溃,即使密钥没有添加到 info.plist,有一些弹出窗口可以提醒用户在 info.plist 中添加相关密钥。任何帮助将不胜感激。

您可以通过阅读info.plist文件来检查是否通过以下代码添加了密钥。

if let permission = Bundle.main.object(forInfoDictionaryKey: "NSCameraUsageDescription") as? String {
   print(permission)
}

如果权限不存在,我们将不会添加请求代码并显示一些提示,如请添加权限。

另外,有一篇很好的文章可以帮助在创建构建之前识别缺少的权限。

https://medium.com/rosberryapps/swift-plist-and-two-smoking-scripts-94bb54cbeded