PHPhotoLibrary.requestAuthorization()不触发iOS 9上的授权提示

我有以下function,显示一个操作表有两个不同的选项。 目前,唯一实施的选项是标题为“照片”。 操作表正确显示,正确的操作被调用。 我的问题是,在模拟器和实际设备上,我不能显示请求访问照片库的提示。

PHPhotoLibrary.authorizationStatus()返回.Denied并呈现通知当前应用程序无法访问照片的模式: 在这里输入图像说明

 @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) } 

我已经“清理”了项目并重置了模拟器设置,但仍然没有得到显示提示。

发生这种情况的原因是Info.plist文件中的CFBundleDisplayName键值为空,并且警报中提示访问照片库的权限消息无法构build。 添加以下行,清理项目并再次构build可以解决问题:

 <?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> 

感谢弗拉基米尔Gorbenko帮助我解决这个问题。

转到Info.plist并添加Bundle显示名称。然后添加代码。

在这里输入图像说明