iOS:请求访问相机

iOS: Request Access to Camera

我有一个带有二维码扫描器的应用程序,运行良好,但在 iOS 8 上,默认访问相机是 "Denied"。因此,我必须进入设置并手动授予应用访问权限才能使用相机。我怎样才能做出类似 "Would you like to give this app access to use the Camera"?

的提示

这是我的代码示例,用于检查相机权限,然后在用户未授予权限时请求权限。但是,授予权限的 link 永远不会出现,最终只会显示 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];
        }
    }];
}
}

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

将用户发送到您的设置,在该设置中可以使用此功能启用相机(在 iOS8 上):

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

如果您正在测试,请尝试从 phone 中删除应用程序,然后重新安装并 运行。这会让您回到 AVAuthorizationStatusNotDetermined 状态。

对于 swift 3 :

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

更多观看此视频https://www.youtube.com/watch?v=Btd1XH-gHKM