首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS:请求访问摄像头

iOS:请求访问摄像头
EN

Stack Overflow用户
提问于 2015-08-31 20:59:30
回答 2查看 14.1K关注 0票数 14

我有一个带有二维码扫描仪的应用程序,它工作得很好,但在iOS 8上,对摄像头的默认访问是“拒绝”。因此,我必须进入设置并手动为应用程序提供使用摄像头的权限。我如何才能使提示符显示类似于“您是否要授予此应用程序使用摄像头的访问权限”?

这是我的代码示例,检查相机权限,然后在用户没有提供权限的情况下请求权限。但是,授予权限的链接从未出现,最终只显示了UIAlertView。当我测试时,状态确实是拒绝的,那么它不请求权限有什么原因吗?谢谢!

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

代码语言:javascript
复制
-(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];
        }
    }];
}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-01 20:37:11

听起来这个应用程序已经被拒绝访问摄像头。在这种情况下,您不能再次提示。每次安装只能提示用户访问一次。在此之后,您需要将用户定向到设置。

将用户发送到您的设置,在您的设置中,可以使用以下设置启用摄像头(在iOS8上):

代码语言:javascript
复制
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

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

票数 16
EN

Stack Overflow用户

发布于 2016-12-02 21:04:41

对于swift 3:

代码语言:javascript
复制
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)

有关更多信息,请查看此视频https://www.youtube.com/watch?v=Btd1XH-gHKM

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32311887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档