我有一个带有二维码扫描仪的应用程序,它工作得很好,但在iOS 8上,对摄像头的默认访问是“拒绝”。因此,我必须进入设置并手动为应用程序提供使用摄像头的权限。我如何才能使提示符显示类似于“您是否要授予此应用程序使用摄像头的访问权限”?
这是我的代码示例,检查相机权限,然后在用户没有提供权限的情况下请求权限。但是,授予权限的链接从未出现,最终只显示了UIAlertView。当我测试时,状态确实是拒绝的,那么它不请求权限有什么原因吗?谢谢!
我还有#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];
}
}];
}
}发布于 2015-09-01 20:37:11
听起来这个应用程序已经被拒绝访问摄像头。在这种情况下,您不能再次提示。每次安装只能提示用户访问一次。在此之后,您需要将用户定向到设置。
将用户发送到您的设置,在您的设置中,可以使用以下设置启用摄像头(在iOS8上):
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];如果您正在测试,请尝试从手机中删除该应用程序,然后重新安装并运行它。这会使您返回到AVAuthorizationStatusNotDetermined状态。
发布于 2016-12-02 21:04:41
对于swift 3:
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)有关更多信息,请查看此视频https://www.youtube.com/watch?v=Btd1XH-gHKM
https://stackoverflow.com/questions/32311887
复制相似问题