【发布时间】: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