【问题标题】:iOS: Request Access to CameraiOS:请求访问相机
【发布时间】:2015-08-31 12:59:30
【问题描述】:

我有一个带有二维码扫描仪的应用程序可以正常工作,但在 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];
        }
    }];
}
}

【问题讨论】:

标签: ios objective-c ios8 camera avfoundation


【解决方案1】:

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

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

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

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

【讨论】:

  • 这是一个巧妙的小技巧,我没有意识到我可以将用户从我的应用程序重定向到设置。这会奏效,我只是不喜欢在警报中告诉他们方向。谢谢! **还有什么方法可以让我直接将它们发送到应用程序的设置,而不是仅仅打开设置应用程序?
  • 目前,即使应用程序被删除,设备也会“记住”隐私设置。因此,除了重置所有设备的隐私设置之外,没有办法再次获得原始弹出窗口。
【解决方案2】:

对于 Swift 3:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多