簡體   English   中英

當用戶允許iOS中的攝像頭訪問警報時如何獲得通知?

[英]How to get notified when user allows the camera access alert in iOS?

我想在倒數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];
    }

對於AVAuthorizationStatusNotDetermined狀態,添加鍵的觀察者(布爾值),並在requestAccessForMediaType完成時更新此標志,然后啟動camera或對用戶的動作調用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];
    }
}

AVAuthorizationStatusNotDetermined情況下,請調用askForPermission

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM