美文网首页
iOS检测相册权限

iOS检测相册权限

作者: 肉肉要次肉 | 来源:发表于2022-02-22 11:15 被阅读0次

1、引入头文件#import<Photos/Photos.h>

#pragma mark-- 检测是否开启相册权限

-(void)photoPower{

    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

        dispatch_async(dispatch_get_main_queue(), ^{

        if (status == PHAuthorizationStatusAuthorized) {//开启

            //开启后的操作

        }else{

               //注,这里一定要回归的主线程操作UI

            dispatch_async(dispatch_get_main_queue(), ^{

                UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:@"提示"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];

                [alertControlleraddAction:cancelAction];

                [alertControlleraddAction:okAction];

                [selfpresentViewController:alertControlleranimated:YEScompletion:nil];

            });

        }

        });

    }];

}

相关文章

网友评论

      本文标题:iOS检测相册权限

      本文链接:https://www.haomeiwen.com/subject/enqglrtx.html