[英]How to get notified when user allows the camera access alert in iOS?
I want to record a video after a count down 3 2 1. But on first time without allowing the camera the video has started recording without showing to camera . 我想在倒数3 2 1后录制视频。但是第一次在没有允许相机的情况下,视频已经开始录制而没有显示给相机。 I want to call the recording code in the block which is called when the user allow to camera on camera access alert.
我想在用户允许摄像机进入摄像机访问警报时调用的块中调用记录代码。
self.recordingManager.previewLayer.frame = CGRectMake(47, 2000, 280, 154);
// set code for count down
imageArr = [[NSArray alloc]initWithObjects:@"countdown_three",@"countdown_two",@"countdown_one", nil];
[_countImageView setImage:[UIImage imageNamed:[imageArr objectAtIndex:0]]];
[_countImageView setHidden:NO];
NSLog(@"%@",[imageArr objectAtIndex:0]);
count = 4;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(setCountDownDuration) userInfo:nil repeats:YES];
// customize progressbar
_captureProgress.hidden = NO;
_captureProgress.progress = 0.0;
_captureProgress.layer.borderWidth = 0.2;
_captureProgress.layer.borderColor = [UIColor colorWithRed:167.0f/255 green:188.0f/255 blue:219.0f/255 alpha:1].CGColor;
CGAffineTransform transform = CGAffineTransformMakeScale(1.6f, 8.0f);
_captureProgress.transform = transform;
count--;
if (count == 3) {
[self.progressStatusLbl setText:@"GET READY"];
[_countImageView setImage:[UIImage imageNamed:[imageArr objectAtIndex:0]]];
}
if (count == 2) {
[_countImageView setImage:[UIImage imageNamed:[imageArr objectAtIndex:1]]];
}
if (count == 1) {
[_countImageView setImage:[UIImage imageNamed:[imageArr objectAtIndex:2]]];
}
if (count == 0) {
[timer invalidate];
timer = nil;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == AVAuthorizationStatusAuthorized)
{
[self.progressStatusLbl setText:@"RECORDING"];
[self openCamera];
}
else if(authStatus == AVAuthorizationStatusNotDetermined)
{
NSLog(@"%@", @"Camera access not determined. Ask for permission.");
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
{
if(granted)
{
NSLog(@"Granted access to %@", AVMediaTypeVideo);
[self.progressStatusLbl setText:@"RECORDING"];
[self openCamera];
}
else
{
NSLog(@"Not granted access to %@", AVMediaTypeVideo);
[self camDenied];
}
}];
}
else if (authStatus == AVAuthorizationStatusRestricted)
{
// My own Helper class is used here to pop a dialog in one simple line.
//[Helper popAlertMessageWithTitle:@"Error" alertText:@"You've been restricted from using the camera on this device. Without camera access this feature won't work. Please contact the device owner so they can give you access."];
[Utils showAlertMessageWithTitle:@"Error" msg:@"You've been restricted from using the camera on this device. Without camera access this feature won't work. Please contact the device owner so they can give you access." firstButtonTitle:@"Ok" secontButtonTitle:nil];
}
else
{
[self camDenied];
}
For AVAuthorizationStatusNotDetermined
state, add observer for a key (boolean), and update this flag in completion of requestAccessForMediaType
then start camera or call camDenied deoending on user's actions. 对于
AVAuthorizationStatusNotDetermined
状态,添加键的观察者(布尔值),并在requestAccessForMediaType
完成时更新此标志,然后启动camera或对用户的动作调用camDenied deoending。
So you code should look like as below: 因此,您的代码应如下所示:
Add a new private property in your class 在课程中添加新的私人财产
@property (nonatomic, strong) NSNumber *granted;
then add following methods 然后添加以下方法
- (void)askForPermission {
NSLog(@"%@", @"Camera access not determined. Ask for permission.");
[self addObserver:self forKeyPath:@"granted" options:NSKeyValueObservingOptionNew context:nil];
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
{
self.granted = @(granted);
}];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"granted"]) {
BOOL granted = [object boolValue];
if(granted)
{
NSLog(@"Granted access to %@", AVMediaTypeVideo);
[self.progressStatusLbl setText:@"RECORDING"];
[self openCamera];
}
else
{
NSLog(@"Not granted access to %@", AVMediaTypeVideo);
[self camDenied];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
call askForPermission
in AVAuthorizationStatusNotDetermined
case. 在
AVAuthorizationStatusNotDetermined
情况下,请调用askForPermission
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.