iOS:请求访问相机

时间:2015-08-31 12:59:30

标签: ios objective-c ios8 camera avfoundation

我有一个带有QR码扫描仪的应用程序可以正常工作,但在iOS 8上,相机的默认访问权限为#34;拒绝"。因此,我必须进入设置并手动授予应用程序访问权限以使用相机。我怎样才能提出类似&#34的提示;你想让这个应用程序访问使用相机"?

以下是我的代码检查相机权限,然后在用户未提供权限时请求权限的示例。但是,提供权限的链接永远不会显示,最终只显示UIAlertView。我测试时状态确实是DENIED,所以有没有理由不要求权限?谢谢!

我也有#import AVFoundation / AVFoundation.h,所以这不是问题。

-(void) checkCameraAuthorization {

AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

if(status == AVAuthorizationStatusAuthorized) { // authorized
    NSLog(@"camera authorized");
}
else if(status == AVAuthorizationStatusDenied){ // denied
    if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType: completionHandler:)]) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            // Will get here on both iOS 7 & 8 even though camera permissions weren't required
            // until iOS 8. So for iOS 7 permission will always be granted.

            NSLog(@"DENIED");

            if (granted) {
                // Permission has been granted. Use dispatch_async for any UI updating
                // code because this block may be executed in a thread.
                dispatch_async(dispatch_get_main_queue(), ^{
                    //[self doStuff];
                });
            } else {
                // Permission has been denied.
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Authorized" message:@"Please go to Settings and enable the camera for this app to use this feature." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
                [alert show];
            }
        }];
    }
}
else if(status == AVAuthorizationStatusRestricted){ // restricted
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Authorized" message:@"Please go to Settings and enable the camera for this app to use this feature." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
}
else if(status == AVAuthorizationStatusNotDetermined){ // not determined

    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        if(granted){ // Access has been granted ..do something
            NSLog(@"camera authorized");
        } else { // Access denied ..do something
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Authorized" message:@"Please go to Settings and enable the camera for this app to use this feature." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
        }
    }];
}
}

2 个答案:

答案 0 :(得分:14)

听起来该应用已被拒绝访问相机。在这种情况下,您无法再次提示。每次安装时,您只能提示用户访问一次。之后,您需要将用户定向到设置。

将用户发送到可以启用相机的设置(在iOS8上):

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

如果您正在测试,请尝试从手机中删除该应用,然后再次安装并运行该应用。这会使您回到AVAuthorizationStatusNotDetermined状态。

答案 1 :(得分:2)

对于swift 3:

UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)

了解更多详情https://www.youtube.com/watch?v=Btd1XH-gHKM