iOS:请求访问相机

14

我有一个带有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];
        }
    }];
}
}

在此处回答:https://dev59.com/iGAf5IYBdhLWcg3w0Vi7 - Samhan Salahuddin
2个回答

16

听起来这个应用程序已经被拒绝访问相机了。在这种情况下,您无法再次发出提示。每次安装后,您只能提示用户访问一次。之后,您需要指导用户前往设置。

将用户发送到您的设置页面,在 iOS8 上可以通过以下方式启用相机:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
如果您在测试,请尝试从手机中删除应用程序,然后再安装并再次运行它。这会将您重新置于“AVAuthorizationStatusNotDetermined”状态。

如果您在测试,请尝试从手机中删除应用程序,然后再安装并再次运行它。这会将您重新置于“AVAuthorizationStatusNotDetermined”状态。


1
这是一个很棒的小技巧,我没想到我可以从我的应用程序将用户重定向到设置。这会起作用,我只是不太喜欢在警报中告诉他们方向。谢谢!另外,有没有办法让他们直接进入应用程序的设置,而不仅仅是打开设置应用程序? - MSU_Bulldog
1
目前,即使删除了应用程序,设备仍会“记住”隐私设置。因此,除了重置所有设备的隐私设置外,没有办法再次获取原始弹出窗口。 - bauerMusic

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接