session-ios/Signal/src/util/UIViewController+CameraPermissions.m

77 lines
3.3 KiB
Mathematica
Raw Normal View History

2016-11-04 23:41:37 +01:00
//
2017-05-05 18:39:21 +02:00
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
2016-11-04 23:41:37 +01:00
//
2017-05-05 18:39:21 +02:00
2016-11-04 23:41:37 +01:00
#import "UIUtil.h"
#import "UIViewController+CameraPermissions.h"
#import <AVFoundation/AVFoundation.h>
2017-05-05 18:39:21 +02:00
#import "Signal-Swift.h"
2016-11-04 23:41:37 +01:00
NS_ASSUME_NONNULL_BEGIN
@implementation UIViewController (CameraPermissions)
- (void)ows_askForCameraPermissions:(void (^)())permissionsGrantedCallback
2016-11-04 23:41:37 +01:00
{
[self ows_askForCameraPermissions:permissionsGrantedCallback failureCallback:nil];
}
- (void)ows_askForCameraPermissions:(void (^)())permissionsGrantedCallback
failureCallback:(nullable void (^)())failureCallback
{
// Avoid nil tests below.
if (!failureCallback) {
failureCallback = ^{
};
}
2016-11-04 23:41:37 +01:00
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
DDLogError(@"Camera ImagePicker source not available");
failureCallback();
2016-11-04 23:41:37 +01:00
return;
}
2016-11-04 23:41:37 +01:00
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (status == AVAuthorizationStatusDenied) {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"MISSING_CAMERA_PERMISSION_TITLE", @"Alert title")
message:NSLocalizedString(@"MISSING_CAMERA_PERMISSION_MESSAGE", @"Alert body")
preferredStyle:UIAlertControllerStyleAlert];
NSString *settingsTitle = NSLocalizedString(@"OPEN_SETTINGS_BUTTON", @"Button text which opens the settings app");
UIAlertAction *openSettingsAction = [UIAlertAction actionWithTitle:settingsTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
2017-05-05 18:39:21 +02:00
[[UIApplication sharedApplication] openSystemSettings];
failureCallback();
}];
[alert addAction:openSettingsAction];
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
failureCallback();
}];
[alert addAction:dismissAction];
[self presentViewController:alert animated:YES completion:nil];
2016-11-04 23:41:37 +01:00
} else if (status == AVAuthorizationStatusAuthorized) {
permissionsGrantedCallback();
} else if (status == AVAuthorizationStatusNotDetermined) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{
if (granted) {
permissionsGrantedCallback();
} else {
failureCallback();
}
});
}];
2016-11-04 23:41:37 +01:00
} else {
DDLogError(@"Unknown AVAuthorizationStatus: %ld", (long)status);
failureCallback();
2016-11-04 23:41:37 +01:00
}
}
@end
NS_ASSUME_NONNULL_END