PHPhotoLibrary requestAuthorization,而不是请求

为了测试,我试图重新创建系统的“请求访问”弹出体验。

更新:
在iOS 11下,删除应用程序后,系统弹出窗口将再次显示。


(上一个问题)

第一次运行App(并且是唯一一次),系统弹出窗口显示,请求访问。 之后, 甚至没有删除应用程序并重新启动设备将再次触发该弹出窗口。

换句话说,设备“记住”用户请求,并且无法重置它。

这是代码:

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { switch (status) { case PHAuthorizationStatusAuthorized: NSLog(@"PHAuthorizationStatusAuthorized"); break; case PHAuthorizationStatusDenied: NSLog(@"PHAuthorizationStatusDenied"); break; case PHAuthorizationStatusNotDetermined: NSLog(@"PHAuthorizationStatusNotDetermined"); break; case PHAuthorizationStatusRestricted: NSLog(@"PHAuthorizationStatusRestricted"); break; } }]; 

当设置中的访问权限关闭时,它将继续打印“PHAuthorizationStatusDenied”。 但不会出现任何弹出窗口。 立即返回。

建议在plist中添加“Bundle display name”。 尝试无效,空值,$(PRODUCT_NAME)和不同的字符串。

清理项目,删除DrivedData(并每次从模拟器中删除App)。 没有运气。

更多信息:

一旦您在“设置”中关闭照片访问权限,Apple示例代码“SamplePhotosApp”就会崩溃。

经过进一步阅读,这似乎是设计的。

来自Apple:

此方法始终立即返回。 如果用户先前已授予或拒绝照片库访问权限,则在调用时执行处理程序块; 否则,它仅在用户响应警报后显示警报并执行该块。

如果用户提示一次,说’此方法总是立即返回’。 之后,它不会再显示请求。 似乎没有办法(但有些自定义消息)再次使用系统消息。

重要的是将此添加到您的应用程序Info.plist文件。 “隐私 – 照片库使用说明”

我使用过类似的东西:“用于访问照片库中的照片。”

此描述将显示在请求的访问警告框中。 可以根据需要进行本地化。

 + (void)imageLibraryCheckAccess:(UIViewController *)presenting handler:(void (^)(PHAuthorizationStatus status))handler { PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; if (status == PHAuthorizationStatusNotDetermined) { [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { if (status != PHAuthorizationStatusAuthorized) { if (handler != nil) handler(status); NSString *title = NSLocalizedStringWithDefaultValue(@"photo.library.access.disabled.alert.title", @"Localizable", [NSBundle mainBundle], @"Photos access", @"Alert title."); NSString *text = NSLocalizedStringWithDefaultValue(@"photo.library.access.disabled.alert.text", @"Localizable", [NSBundle mainBundle], @"You explicitly disabled photo library access. This results in inability to work with photos.", @"Alert text."); [self alertWithPresenting:presenting title:title text:text buttons:@[[L10n okButton]] handler:nil]; } else if (status == PHAuthorizationStatusAuthorized) { if (handler != nil) handler(status); } }]; } else if (status != PHAuthorizationStatusAuthorized) { if (handler != nil) handler(status); NSString *title = NSLocalizedStringWithDefaultValue(@"photo.library.access.notauthorized.alert.title", @"Localizable", [NSBundle mainBundle], @"Photos access", @"Alert title."); NSString *text = NSLocalizedStringWithDefaultValue(@"photo.library.access.notauthorized.alert.text", @"Localizable", [NSBundle mainBundle], @"Photo library access is disabled. Please check the application permissions or parental control settings in order to work with photos.", @"Alert text."); [self alertWithPresenting:presenting title:title text:text buttons:@[[L10n okButton]] handler:nil]; } else if (status == PHAuthorizationStatusAuthorized) { if (handler != nil) handler(status); } } 

以下是我如何使用它:

 [YourClassName imageLibraryCheckAccess:self handler:^(PHAuthorizationStatus status) { if (status == PHAuthorizationStatusAuthorized) { } }]; 

祝你好运!