2016-07-28 50 views
4

我有一个样品的iOS 10应用程序请求授权的照片库,并与下列崩溃错误而崩溃的真实设备上:崩溃请求授权照片库中的iOS 10

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

回购可发现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") 
     }) 
    } 
} 

回答

8

我已经foun d问题与iOS 10中一些使用说明键已成为强制性相关的事实相关。

虽然NSPhotoLibraryUsageDescription自iOS 6开始就已经出现,但它仅在iOS 10中成为需求,并且崩溃消息wasn对我非常有帮助。

在最新的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.