11

My app keeps crashing when running in the simulator everytime I try to request authorization for the photo library. I am using the following code in my appDelegate in didFinishLaunchingWithOptions:

if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.authorized {
     PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) in

     })
}

Using xcode 8 beta with swift 3.0.

alionthego
  • 8,508
  • 9
  • 52
  • 125

2 Answers2

24

In my testing, iOS 10 doesn't like to output useful error messages unless you're running on an actual device. In this particular case, you probably haven't provided the key NSPhotoLibraryUsageDescription in your Info.plist file, and that value must be provided before requesting authorization.

TwoStraws
  • 12,862
  • 3
  • 57
  • 71
  • Thanks for your reply. I haven't spent too much time modifying my plist file. Is it as simple as pressing the plus and manually typing NSPhotoLibraryUsageDescription and selecting the type to String?. Also will this cause issues going forward? I thought that that property was a system property and could not be modified within an app other than by system alert acceptance. – alionthego Jun 17 '16 at 22:33
  • 2
    Do those steps you said, then enter some text in the box to the right: "We need to read your photos so that we can grok some glerbs." This will be shown to the user to explain why you want access, and it's standard procedure in iOS. [I'm writing a book on iOS 10 right now](https://gumroad.com/l/ios10) and the first chapter covers exactly this procedure. – TwoStraws Jun 17 '16 at 22:37
  • That's great. Works perfectly. Thanks very much for your help. – alionthego Jun 17 '16 at 22:56
  • Meanwhile I compile this, I would like to say thank you because I know this will solve my issue as well. Edit: It did! – f0rz Jul 20 '16 at 13:57
  • Any way to localize the value? – LinusGeffarth Sep 20 '16 at 13:47
3

Have to allow access to photos on device. Add below key and string to your info.plist. The autocomplete in the property list view is "Privacy - Photo Library Usage Description". Or just open your info.plist in source code view and add the following:

<key>NSPhotoLibraryUsageDescription</key>
<string>We need access to your photos.</string>
kelsheikh
  • 1,278
  • 3
  • 17
  • 37