相机权限以及引导设置

在第一次使用相机前,系统会弹出提示“是否允许使用相机”,如果我们拒绝权限请求的话,就无法正常使用相机。下面是相机权限状态的判断,以及相关的引导设置。

#pragma mark 相机权限
- (void)cameraPrivilege
{
    NSString *mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if(authStatus ==AVAuthorizationStatusRestricted)
    {//用户还没有处理权限请求
        NSLog(@"Restricted");
    }
    else if(authStatus == AVAuthorizationStatusDenied)
    {//用户已拒绝权限请求
        [self goToSettingCameraPrivilege];
        return;
    }
    else if(authStatus == AVAuthorizationStatusAuthorized)
    {//允许权限请求
    }
    else if(authStatus == AVAuthorizationStatusNotDetermined)
    {
        [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
            if(granted)
            {//用户点击允许权限请求
            }
            else
            {//用户点击拒绝权限请求
                [self goToSettingCameraPrivilege];
            }
        }];
    }
    else
    {
        //未知的权限状态
    }
}

#pragma mark 提示用户去系统设置修改相机权限
- (void)goToSettingCameraPrivilege
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"相机权限已被禁用,基础功能暂无法使用,是否去开启?" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"去开启" style:0 handler:^(UIAlertAction * _Nonnull action) {
            NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            if([[UIApplication sharedApplication] canOpenURL:settingsURL])
            {
                [[UIApplication sharedApplication] openURL:settingsURL];
            }
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"暂不开启" style:1 handler:^(UIAlertAction * _Nonnull action) {

        }];
        [alertController addAction:confirm];
        [alertController addAction:cancel];
        [self presentViewController:alertController animated:YES completion:nil];
    }];
}

你可能感兴趣的:(相机权限以及引导设置)