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
|
|
|
|
|
|
|
#import "Signal-Swift.h"
|
2017-11-08 20:04:51 +01:00
|
|
|
#import "UIUtil.h"
|
2017-11-07 16:32:28 +01:00
|
|
|
#import "UIViewController+Permissions.h"
|
2017-11-08 20:04:51 +01:00
|
|
|
#import <AVFoundation/AVFoundation.h>
|
2017-11-07 16:38:36 +01:00
|
|
|
#import <SignalServiceKit/Threading.h>
|
2016-11-04 23:41:37 +01:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2017-11-07 16:32:28 +01:00
|
|
|
@implementation UIViewController (Permissions)
|
2016-11-04 23:41:37 +01:00
|
|
|
|
2017-11-07 16:38:36 +01:00
|
|
|
- (void)ows_askForCameraPermissions:(void (^)(BOOL granted))callbackParam
|
2017-06-09 00:10:02 +02:00
|
|
|
{
|
2017-11-07 16:32:28 +01:00
|
|
|
DDLogVerbose(@"[%@] ows_askForCameraPermissions", NSStringFromClass(self.class));
|
2017-11-07 16:04:58 +01:00
|
|
|
|
2017-11-07 16:38:36 +01:00
|
|
|
// Ensure callback is invoked on main thread.
|
|
|
|
void (^callback)(BOOL) = ^(BOOL granted) {
|
|
|
|
DispatchMainThreadSafe(^{
|
|
|
|
callbackParam(granted);
|
|
|
|
});
|
|
|
|
};
|
2017-06-09 00:10:02 +02:00
|
|
|
|
2017-11-07 16:04:58 +01:00
|
|
|
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
|
|
|
|
DDLogError(@"Skipping camera permissions request when app is not active.");
|
2017-11-07 16:38:36 +01:00
|
|
|
callback(NO);
|
2017-11-07 16:04:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-04 23:41:37 +01:00
|
|
|
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
|
|
|
|
DDLogError(@"Camera ImagePicker source not available");
|
2017-11-07 16:38:36 +01:00
|
|
|
callback(NO);
|
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) {
|
2017-11-07 16:32:28 +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) {
|
|
|
|
[[UIApplication sharedApplication] openSystemSettings];
|
2017-11-07 16:38:36 +01:00
|
|
|
callback(NO);
|
2017-11-07 16:32:28 +01:00
|
|
|
}];
|
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) {
|
2017-11-07 16:38:36 +01:00
|
|
|
callback(NO);
|
2017-06-09 00:10:02 +02:00
|
|
|
}];
|
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) {
|
2017-11-07 16:38:36 +01:00
|
|
|
callback(YES);
|
2016-11-04 23:41:37 +01:00
|
|
|
} else if (status == AVAuthorizationStatusNotDetermined) {
|
2017-06-09 00:10:02 +02:00
|
|
|
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
|
|
|
|
completionHandler:^(BOOL granted) {
|
2017-11-07 16:38:36 +01:00
|
|
|
callback(granted);
|
2017-06-09 00:10:02 +02:00
|
|
|
}];
|
2016-11-04 23:41:37 +01:00
|
|
|
} else {
|
|
|
|
DDLogError(@"Unknown AVAuthorizationStatus: %ld", (long)status);
|
2017-11-07 16:38:36 +01:00
|
|
|
callback(NO);
|
2016-11-04 23:41:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 16:32:28 +01:00
|
|
|
- (void)ows_askForMicrophonePermissions:(void (^)(BOOL granted))callbackParam
|
|
|
|
{
|
|
|
|
DDLogVerbose(@"[%@] ows_askForMicrophonePermissions", NSStringFromClass(self.class));
|
|
|
|
|
|
|
|
// Ensure callback is invoked on main thread.
|
|
|
|
void (^callback)(BOOL) = ^(BOOL granted) {
|
2017-11-07 16:38:36 +01:00
|
|
|
DispatchMainThreadSafe(^{
|
2017-11-07 16:32:28 +01:00
|
|
|
callbackParam(granted);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
|
|
|
|
DDLogError(@"Skipping microphone permissions request when app is not active.");
|
|
|
|
callback(NO);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
|
|
|
|
callback(granted);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2016-11-04 23:41:37 +01:00
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|