iOS10中请求访问照片库时发生崩溃

4

我有一个样例iOS 10应用程序,请求授权访问照片库,并在真实设备上崩溃,出现以下崩溃错误:

PhotosAuthorizationCrashTest[2014:42551] [access] <private>

源代码位于这里

这是请求授权的代码(Swift 3.0):

private func requestAuthorizationIfNeeded() {
    DispatchQueue.main.async {
        let status = PHPhotoLibrary.authorizationStatus()
        if status == .authorized {
            return
        }

        PHPhotoLibrary.requestAuthorization({ (status) in
            if status == .authorized {
                return
            }

            NSLog("Could not get authorization to access photos")
        })
    }
}
1个回答

8
我发现问题与iOS 10中某些使用说明键变为强制性有关。尽管自iOS 6以来就存在NSPhotoLibraryUsageDescription,但它直到iOS 10才成为必需品,并且崩溃消息并没有太大帮助。在最新的Xcode模拟器(目前为Xcode 8 beta 3)中,崩溃消息更加详细,(尽管在设备上仍然相同):

[access] This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.

因此,在我的Info.plist文件中添加NSPhotoLibraryUsageDescription已解决该问题。
有关更多文档,请参见Cocoa Keys
更具体地说,请参阅名为NSPhotoLibraryUsageDescription的部分。
重要提示:为了保护用户隐私,iOS 10.0及以上版本中链接的应用程序如果要访问用户的照片库,必须静态声明其意图。请在您的应用程序的Info.plist文件中包含NSPhotoLibraryUsageDescription键,并为此键提供一个目的字符串。如果您的应用程序尝试访问用户的照片库而没有相应的目的字符串,则您的应用程序将退出。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接