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)
|
|
|
|
|
2016-11-08 22:28:00 +01:00
|
|
|
- (void)ows_askForCameraPermissions:(void (^)())permissionsGrantedCallback
|
2016-11-04 23:41:37 +01:00
|
|
|
{
|
2017-06-09 00:10:02 +02:00
|
|
|
[self ows_askForCameraPermissions:permissionsGrantedCallback failureCallback:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)ows_askForCameraPermissions:(void (^)())permissionsGrantedCallback
|
|
|
|
failureCallback:(nullable void (^)())failureCallback
|
|
|
|
{
|
2017-06-09 13:32:16 +02:00
|
|
|
// Avoid nil tests below.
|
2017-06-09 00:10:02 +02:00
|
|
|
if (!failureCallback) {
|
|
|
|
failureCallback = ^{
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-04 23:41:37 +01:00
|
|
|
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
|
|
|
|
DDLogError(@"Camera ImagePicker source not available");
|
2017-06-09 00:10:02 +02:00
|
|
|
failureCallback();
|
2016-11-04 23:41:37 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-06-09 00:10:02 +02:00
|
|
|
|
2016-11-04 23:41:37 +01:00
|
|
|
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
|
|
|
|
if (status == AVAuthorizationStatusDenied) {
|
2016-11-08 22:28:00 +01:00
|
|
|
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];
|
2017-06-09 00:10:02 +02:00
|
|
|
failureCallback();
|
2016-11-08 22:28:00 +01:00
|
|
|
}];
|
|
|
|
[alert addAction:openSettingsAction];
|
|
|
|
|
2017-07-13 20:53:24 +02:00
|
|
|
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton
|
2016-11-08 22:28:00 +01:00
|
|
|
style:UIAlertActionStyleCancel
|
2017-06-09 00:10:02 +02:00
|
|
|
handler:^(UIAlertAction *action) {
|
|
|
|
failureCallback();
|
|
|
|
}];
|
2016-11-08 22:28:00 +01:00
|
|
|
[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) {
|
2017-06-09 00:10:02 +02:00
|
|
|
[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);
|
2017-06-09 00:10:02 +02:00
|
|
|
failureCallback();
|
2016-11-04 23:41:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|