这里只说iOS8之后的方法了,之前的很少有app做兼容了,微信都是从10开始了。哈哈哈
在做权限判断的时候一般是这样
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted ||
status == PHAuthorizationStatusDenied) {
return NO;
}
return YES;
但是有的时候这样判断出来的权限status一直会是
PHAuthorizationStatusNotDetermined
这是为什么呢,因为iOS11开始,判断权限的方式是异步的,所有,在Apple的文档上面也关于这方面加了说明,出现上述情况的时候,建议使用
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
//操作图片
}else{
//注,这里一定要回归的主线程操作UI
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"相册权限未设置,请开启相册权限" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
});
}
}];
so解决