重要的是将此添加到您的应用程序 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) {
}
}];
祝你好运!