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

问题描述:

我想在倒计时后录制视频3 2 1.但是,在第一次不允许相机的情况下,视频已开始录制而不会显示在相机上。我想在用户允许摄像头访问警报时调用的块中调用录制代码。如何在iOS用户允许摄像头访问警报时得到通知?

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

对于AVAuthorizationStatusNotDetermined状态,在完成requestAccessForMediaType增加观察员的关键(布尔),并更新该标志然后启动相机或致电camDenied对用户的操作deoending。

所以,你的代码看起来应该如下:

添加一个新的私有财产类

@property (nonatomic, strong) NSNumber *granted; 

然后在AVAuthorizationStatusNotDetermined情况下,添加以下方法

- (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