在 iOS 10 中请求对照片库的授权时发生崩溃
Crash requesting authorisation to Photos Library in iOS 10
我有一个示例 iOS 10 应用程序请求对照片库的授权并在真实设备上崩溃并出现以下崩溃错误:
PhotosAuthorizationCrashTest[2014:42551] [access] <private>
可以找到 repo here
这是请求授权的代码(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")
})
}
}
我发现这个问题与 iOS 10 中某些使用说明键已成为强制性这一事实有关。
尽管 NSPhotoLibraryUsageDescription
自 iOS 6 以来就已经存在,但它只是在 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.
所以将 NSPhotoLibraryUsageDescription
添加到我的 Info.plist
文件中解决了这个问题。
有关更多文档,请参阅 Cocoa Keys
更具体地说,名为 NSPhotoLibraryUsageDescription
的部分:
Important: To protect user privacy, an iOS app linked on or after iOS 10.0, and which accesses the user’s photo library, must statically declare the intent to do so. Include the NSPhotoLibraryUsageDescription key in your app’s Info.plist file and provide a purpose string for this key. If your app attempts to access the user’s photo library without a corresponding purpose string, your app exits.
我有一个示例 iOS 10 应用程序请求对照片库的授权并在真实设备上崩溃并出现以下崩溃错误:
PhotosAuthorizationCrashTest[2014:42551] [access] <private>
可以找到 repo here
这是请求授权的代码(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")
})
}
}
我发现这个问题与 iOS 10 中某些使用说明键已成为强制性这一事实有关。
尽管 NSPhotoLibraryUsageDescription
自 iOS 6 以来就已经存在,但它只是在 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.
所以将 NSPhotoLibraryUsageDescription
添加到我的 Info.plist
文件中解决了这个问题。
有关更多文档,请参阅 Cocoa Keys
更具体地说,名为 NSPhotoLibraryUsageDescription
的部分:
Important: To protect user privacy, an iOS app linked on or after iOS 10.0, and which accesses the user’s photo library, must statically declare the intent to do so. Include the NSPhotoLibraryUsageDescription key in your app’s Info.plist file and provide a purpose string for this key. If your app attempts to access the user’s photo library without a corresponding purpose string, your app exits.