我的应用程序默认具有摄像头访问权限。如何将其关闭并请求许可?

时间:2015-06-09 07:29:42

标签: ios objective-c ios8 camera user-permissions

我在我的应用程序中使用相机,我想要求每个其他应用程序提示的时髦许可。我怎么能得到它?

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == AVAuthorizationStatusAuthorized) {
    // do your logic
    authorized = YES;
     [self actionLaunchAppCamera];

} else if(authStatus == AVAuthorizationStatusDenied){
    // denied
    authorized = NO;

} else if(authStatus == AVAuthorizationStatusRestricted){
    // restricted, normally won't happen
} else if(authStatus == AVAuthorizationStatusNotDetermined){
    // not determined?!
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        if(granted){
            NSLog(@"Granted access to %@", AVMediaTypeVideo);



        } else {
            NSLog(@"Not granted access to %@", AVMediaTypeVideo);
        }
    }];
} else {
    // impossible, unknown authorization status
}

我使用上面的代码来检查授权,但它总是进入authorizedstatus条件,因为它已经拥有权限。我已经从隐私设置检查了权限已经开启。但我从来没有要求用户许可。

谢谢大家。

1 个答案:

答案 0 :(得分:1)

编辑:哦,我明白你的意思了。然后,您需要创建AVCaptureDeviceInput以查看之前是否已经询问过权限。如果没有,则会提示用户进行许可。

NSError outError = NULL;
AVCaptureDevice captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
If (captureDevice != NULL) {
AVCaptureDeviceInput captureInput = [AVCaptureDeviceInput deviceInputfromDevice:captureDevice error:outError];
    If (captureInput != NULL && outError == NULL) {
        // we have permission to use the camera
    }
}

您无需卸载该应用即可重置访问权限。假设您的设备运行的是iOS 8,只需转到相机设置屏幕并禁用访问权限:

  1. 退出应用
  2. 转到设置 - >隐私 - >相机
  3. 查找关闭开关的应用
  4. 运行应用