简体   繁体   English

当用户允许iOS中的摄像头访问警报时如何获得通知?

[英]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.

相关问题 当用户选择允许访问iOS中的Camera时收到通知 - Get notified when the user makes selection for allowing access to Camera in iOS 用户在iOS中启动消息传递或邮件发送应用程序时如何获得通知 - How do I get notified when user starts messaging or mailing app in iOS IOS:如何在用户拒绝时获取CLLocationManager警报 - IOS: How to get CLLocationManager alert when user declined iOS 9 Swift 2.0如何获取通知,以便当用户向另一用户发送消息时得到通知? - iOS 9 Swift 2.0 How can I get notification so when a user sends a message to another user they get notified? Xamarin Forms iOS中的网络连接更改时如何获得通知 - How to get notified when network connection changes in Xamarin Forms iOS 如何在 iOS 收到新短信时收到通知 - How to get notified when new SMS is received in iOS 在iOS中连接或断开wifi时如何获取通知? - How to get notified when wifi gets connected or disconnected in iOS? iOS - 如何在AFHTTPClient完成所有操作时收到通知 - iOS - How to get notified when AFHTTPClient is done with all operations 如何在iOS中卸载我们的应用程序时收到通知 - How to get notified when our app is uninstalled in iOS iOS:在返回UITableViewCell时获得通知 - iOS: Get notified when UITableViewCell is returned
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM