1.
#import <PhotosUI/PhotosUI.h>
Info.plist 中设置
“PHPhotoLibraryPreventAutomaticLimitedAccessAlert”的值为 YES 来阻止该弹窗反复弹出
2.iOS14之后获取相册授权状态的方法
if (@available(iOS 14, *)) { [PHPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite handler:^(PHAuthorizationStatus status) { dispatch_async(dispatch_get_main_queue(), ^{ ///用户已授权此应用程序有限的照片库访问权限 if (status == PHAuthorizationStatusLimited) { }else{ } }); }]; }
3.第一种使用方法,和QQ一样,推荐使用
不在 Info.plist 阻止弹窗弹出, 使用监听获取回调
1. 实现PHPhotoLibraryChangeObserver @interface AddWatermarkViewController ()<PHPhotoLibraryChangeObserver> 2. 注册监听 - (void)viewDidLoad { [super viewDidLoad]; [[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self]; }
/// PHPhotoLibrary监听: 选择图片之后的回调
#pragma mark- PHPhotoLibraryChangeObserver
- (void)photoLibraryDidChange:(PHChange *)changeInstance{
///一定要在主线程刷新:这里是异步监听
dispatch_async (dispatch_get_main_queue (), ^{
[self reloadSelectedAssetCollection:nil];
});
}
4. 注销监听 - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self]; }
2.自定义控制器,此方法选了之后去 设置中查看, 还是没效果。。 原因待查
1. 设置代理 PHPickerViewControllerDelegate @interface AddWatermarkViewController ()< PHPickerViewControllerDelegate> 2.手动显示弹窗 - (void)showAlert{ @WeakObj(self); if (@available(iOS 14, *)) {
///14之后想着授权状态方法:异步的 [PHPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite handler:^(PHAuthorizationStatus status) { dispatch_async(dispatch_get_main_queue(), ^{ @StrongObj(self); if (status == PHAuthorizationStatusLimited) { PHPickerConfiguration *configuration = [[PHPickerConfiguration alloc] init]; configuration.filter = [PHPickerFilter imagesFilter]; // 可配置查询用户相册中文件的类型,支持三种 imagesFilter 表示 只显示图片 configuration.selectionLimit = 0; // 默认为1,为0时表示可多选。 PHPickerViewController *picker = [[PHPickerViewController alloc] initWithConfiguration:configuration]; picker.delegate = self; [self presentViewController:picker animated:YES completion:nil]; } }); }]; } } //点击取消或者完成的回调 - (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPickerResult *> *)results API_AVAILABLE(ios(14)){ [picker dismissViewControllerAnimated:YES completion:nil]; if (!results || !results.count) { return; } [self reloadSelectedAssetCollection:self.selectedAssetCollection]; NSLog(@"didFinishPicking"); }
参考:
iOS14 适配 先行者
https://www.jianshu.com/p/a0b0303962bb
iOS14 隐私适配及部分解决方案
https://www.jianshu.com/p/1803bd950b90?devtype=android