MC. MC. - 6 months ago 34
iOS Question

PHPhotoLibrary.requestAuthorization() not triggering authorization prompt on iOS 9

I have the following function that displays an action sheet with two different options. Currently, the only implemented option is the titled "Photos". The action sheet is presented correctly and the proper action gets called. My problem is that on the simulator and on an actual device, I can not get to display the prompt requesting access to the photo library.

PHPhotoLibrary.authorizationStatus()
returns
.Denied
and presents the modal informing the current app does not have access to the photos:
enter image description here

@IBAction func attachPhotoButtonTapped(sender: AnyObject) {
let actionSheetController = UIAlertController(title: "Images", message: "Select image source...", preferredStyle: .ActionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) {
(action) in
// ...
}
actionSheetController.addAction(cancelAction)

let takePictureAction = UIAlertAction(title: "Camera", style: .Default) {
(action) in
// ...
}
actionSheetController.addAction(takePictureAction)

let choosePictureAction = UIAlertAction(title: "Photos", style: .Default) {
(action) in
PHPhotoLibrary.requestAuthorization({(status:PHAuthorizationStatus) in
switch status{
case .Authorized:
dispatch_async(dispatch_get_main_queue(), {
print("Authorized")
})
break
case .Denied:
dispatch_async(dispatch_get_main_queue(), {
print("Denied")
})
break
default:
dispatch_async(dispatch_get_main_queue(), {
print("Default")
})
break
}
})
}
actionSheetController.addAction(choosePictureAction)

presentViewController(actionSheetController, animated: true, completion: nil)
}


I've already "cleaned" the project and reset the simulator settings, but still not getting the prompt to get displayed.

MC. MC.
Answer

This happens because the CFBundleDisplayName key in your Info.plist file has an empty value and the message in the alert that prompts for permission to access the Photo Library cannot be constructed. Adding the following lines, cleaning the project and building again fixes the problem:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDisplayName</key>
    <string>$(PRODUCT_NAME)</string>
    <!-- More keys and values -->
</dict>
</plist>

Thanks to Vladimir Gorbenko for helping me solve this.