相册权限 第一次安装、用户是否授权

时间:2021-06-02 16:26:52

void (^allowEvent)() = ^{

        UIImagePickerControllerSourceType sourceType;

        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        UIImagePickerController *picker = [[UIImagePickerController alloc]init];

        picker.delegate = self;

        picker.sourceType = sourceType;

        picker.allowsEditing = YES;

        UIViewController *ctl = [[[UIApplication sharedApplication] windows]lastObject].rootViewController;

        [ctl presentViewController:picker animated:YES completion:nil];

    };

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

        PHAuthorizationStatus author = [PHPhotoLibrary authorizationStatus];

        if (author == PHAuthorizationStatusNotDetermined) {

            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

                dispatch_async(dispatch_get_main_queue(), ^{

                    if (status == PHAuthorizationStatusAuthorized) {

                        allowEvent();

                    }else{

                        showAlert(@"用户取消相册授权,请在设置中启用");

                    }

                });

            }];

            return;

        }else if(author == PHAuthorizationStatusRestricted || author == PHAuthorizationStatusDenied){

            showAlert(@"相册权限受限,请在设置中启用");

            return;

        }

        allowEvent();

    }