iOS:请求访问相机

MSU*_*dog 14 camera objective-c avfoundation ios ios8

我有一个带有QR码扫描仪的应用程序可以正常工作,但在iOS 8上,对相机的默认访问是"拒绝".因此,我必须进入设置并手动授予应用程序访问权限以使用相机.如何提示"你想让这个应用程序访问使用相机"这样的提示吗?

以下是我的代码检查相机权限的示例,然后在用户未提供权限时请求权限.但是,提供权限的链接永远不会显示,最终只显示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];
        }
    }];
}
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*ney 16

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

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

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
Run Code Online (Sandbox Code Playgroud)

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