iOS中获取相册、相机、定位、以及麦克风权限设置

1、判断用户是否获取了相册的访问权限

#import 

// 获取相册权限
- (void)getPhotoLibraryAuthor {
    ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
    if (status == ALAuthorizationStatusRestricted || status == ALAuthorizationStatusDenied) {
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"请开启相册权限\n 设置 -> 隐私 -> 照片" preferredStyle:UIAlertControllerStyleAlert];
        [alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alertVc animated:YES completion:nil];

    } else {
        [self openPhotoLibrary];
    }
}

2、判断用户是否获取了相机的访问权限

#import 
#import 

// 获取相机权限
- (void)getCameraAuthor {
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied) {
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"请开启相机访问权限\n 设置 -> 隐私 -> 相机" preferredStyle:UIAlertControllerStyleAlert];
        [alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alertVc animated:YES completion:nil];

    } else {
        [self openCamera];
    }
}

3、判断用户是否获取了麦克风的访问权限

#import 

- (BOOL)getMicrophoneAuthor {
    __block BOOL canRecord = YES;
    if ([self.audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
        [self.audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
            if (granted) {
                canRecord = YES;
            } else {
                canRecord = NO;
                UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"请开启麦克风访问权限:\n设置 -> 隐私 -> 麦克风" preferredStyle:UIAlertControllerStyleAlert];
        [alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alertVc animated:YES completion:nil];
            }
        }];
    }
    return canRecord;
}

4、判断用户是否获取了定位的访问权限

//检测的是整个的iOS系统的定位服务是否开启
[CLLocationManagerlocationServicesEnabled]

//检测当前应用的定位服务是否开启需要通过一下方法来检测
- (void)locationManager:(CLLocationManager*)managerdidFailWithError:(NSError*)error {
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"请开启定位权限:\n设置 -> 隐私 -> 定位服务" preferredStyle:UIAlertControllerStyleAlert];
        [alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alertVc animated:YES completion:nil];
}

**或者是下面这个可以尝试一下:**

- (void)getLocationAuthor {
        CLAuthorizationStatus status = [CLLocationManager authorizationStatus];  
        if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) {  
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"请开启定位权限:\n设置 -> 隐私 -> 定位服务" preferredStyle:UIAlertControllerStyleAlert];
        [alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alertVc animated:YES completion:nil];  
    } 
}

你可能感兴趣的:(iOS)