如何在用户允许iOS中的摄像头访问警报时收到通知?

时间:2017-08-30 12:24:05

标签: ios objective-c

我想在倒计时3 2 1后录制视频。但是在第一次不允许摄像机的情况下,视频已经开始录制而不向摄像机显示。我想调用当用户允许相机进行摄像头访问警报时调用的块中的录制代码。

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];
    }

1 个答案:

答案 0 :(得分:0)

对于AVAuthorizationStatusNotDetermined状态,添加一个键的观察者(布尔值),并在完成requestAccessForMediaType时更新此标志,然后启动摄像头或对用户的操作调用camDenied deoending。

所以你的代码应该如下所示:

在班级中添加新的私人资源

@property (nonatomic, strong) NSNumber *granted;

然后添加以下方法

- (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];
    }
}

askForPermission案例中致电AVAuthorizationStatusNotDetermined