iOS权限检测

AuthManager.h
@interface AuthManager : NSObject

#pragma mark 检测麦克风
+(BOOL)checkMicphoneAuth;
#pragma mark 判断相册权限是否打开
+(BOOL)checkLibraryAuth;
#pragma mark 判断相机权限是否打开
+(BOOL)checkCameraAuth;
#pragma mark 判断地图权限是否打开
+(BOOL)checkLocationAuth;
@end
AuthManager.m

#import 
#import 
@implementation AuthManager

#pragma mark 判断相机权限是否打开
+(BOOL)checkCameraAuth
{
    //相机权限
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus ==AVAuthorizationStatusRestricted ||//此应用程序没有被授权访问的照片数据。可能是家长控制权限
        authStatus ==AVAuthorizationStatusDenied)  //用户已经明确否认了这一照片数据的应用程序访问
    {
        [self showAlert:@"相机未授权" message:[NSString stringWithFormat:@"在“设置-%@”打开相机才能正常使用哦~",MAPP_NAME]];
        return NO;
        
    }
    return YES;
}
/*
#pragma mark 判断相册权限是否打开 
+(BOOL)checkLibraryAuth
{
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status != PHAuthorizationStatusAuthorized) {
        [self showAlert:@"相册未授权" message:[NSString stringWithFormat:@"在“设置-%@”打开相册才能正常使用哦~",MAPP_NAME]];
        return NO;
    }
    return YES;
}
*/
#pragma mark 后来遇到app无法访问相册,设置-隐私-相册里也没有该app,研究了一下,代码优化了一下
#pragma mark 判断相册权限是否打开
+(BOOL)checkLibraryAuth
{
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusAuthorized) {
        return YES;
    }else if (status == PHAuthorizationStatusNotDetermined){
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { //弹出访问权限提示框
            if (status == PHAuthorizationStatusAuthorized) {
            } else {
                [AuthManager showAlert:@"相册未授权" message:[NSString stringWithFormat:@"在“设置-%@”打开相册才能正常使用哦~",MAPP_NAME]];
            };
        }];
    }else {
        [AuthManager showAlert:@"相册未授权" message:[NSString stringWithFormat:@"在“设置-%@”打开相册才能正常使用哦~",MAPP_NAME]];
    }
    return YES;
}

#pragma mark 检测麦克风
+(BOOL)checkMicphoneAuth
{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    if (authStatus ==AVAuthorizationStatusRestricted ||//此应用程序没有被授权访问。可能是家长控制权限
        authStatus ==AVAuthorizationStatusDenied)  //用户已经明确否认了这一数据的应用程序访问
    {
        [self showAlert:@"麦克风未授权" message:[NSString stringWithFormat:@"在“设置-%@”打开麦克风才能正常使用哦~",MAPP_NAME]];
        return NO;
        
    }
    return YES;
}

#pragma mark 判断地图权限是否打开
+(BOOL)checkLocationAuth {
    
    if ([CLLocationManager locationServicesEnabled])
    {
        //system location enabled
        if ([CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse && [CLLocationManager authorizationStatus] !=kCLAuthorizationStatusAuthorizedAlways)
        {
            //定位服务开启 -但是用户没有允许他定位
            [self showAlert:@"地理位置未授权" message:[NSString stringWithFormat:@"在“设置-%@”允许访问位置信息才能正常使用定位功能哦~",MAPP_NAME]];
            return NO;
        }
        return YES;
    }
    else
    {
        //定位服务开启 -但是用户没有允许他定位
        [self showAlert:@"打开定位服务" message:[NSString stringWithFormat:@"请去系统定位服务,允许%@获取您的位置",MAPP_NAME]];
        return NO;
    }
}
+ (void)showAlert:(NSString*)title message:(NSString *)message {
    WeakSelf
    UIAlertController *alertvc = [UIAlertController alertControllerWithTitle:title
                                                                     message:message
                                                              preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * _Nonnull action) {
                                                   }];
    UIAlertAction *setting = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault
                                                    handler:^(UIAlertAction * _Nonnull action) {
                                                        [weakSelf gotoSetting];
                                                    }];
    [alertvc addAction:cancel];
    [alertvc addAction:setting];
    [[AppDelegate getCurrentVC] presentViewController:alertvc animated:YES completion:nil];
}

/**
 跳转到设置页面
 */
+ (void)gotoSetting {
    NSURL *settingUrl = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if (@available(iOS 10.0, *)) {
        #pragma mark 这里的返回只是 跳转到设置页面成功
        [[UIApplication sharedApplication] openURL:settingUrl options:@{}completionHandler:^(BOOL success) {
        }];
    } else {
        // Fallback on earlier versions
        [[UIApplication sharedApplication] openURL:settingUrl];
    }
}

你可能感兴趣的:(iOS权限检测)