mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
4.3 KiB
Matlab
111 lines
4.3 KiB
Matlab
9 years ago
|
//
|
||
8 years ago
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
||
9 years ago
|
//
|
||
8 years ago
|
|
||
|
#import "Signal-Swift.h"
|
||
8 years ago
|
#import "UIUtil.h"
|
||
8 years ago
|
#import "UIViewController+Permissions.h"
|
||
8 years ago
|
#import <AVFoundation/AVFoundation.h>
|
||
9 years ago
|
|
||
|
NS_ASSUME_NONNULL_BEGIN
|
||
|
|
||
8 years ago
|
@implementation UIViewController (Permissions)
|
||
9 years ago
|
|
||
8 years ago
|
- (void)ows_askForCameraPermissions:(void (^)(void))successCallback
|
||
9 years ago
|
{
|
||
8 years ago
|
[self ows_askForCameraPermissions:successCallback failureCallback:nil];
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
- (void)ows_askForCameraPermissions:(void (^)(void))successCallback failureCallback:(nullable void (^)(void))failureCallback
|
||
8 years ago
|
{
|
||
8 years ago
|
DDLogVerbose(@"[%@] ows_askForCameraPermissions", NSStringFromClass(self.class));
|
||
8 years ago
|
|
||
8 years ago
|
// Avoid nil tests below.
|
||
8 years ago
|
if (!failureCallback) {
|
||
|
failureCallback = ^{
|
||
|
};
|
||
|
}
|
||
|
|
||
8 years ago
|
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
|
||
|
DDLogError(@"Skipping camera permissions request when app is not active.");
|
||
|
failureCallback();
|
||
|
return;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
|
||
|
DDLogError(@"Camera ImagePicker source not available");
|
||
8 years ago
|
failureCallback();
|
||
9 years ago
|
return;
|
||
|
}
|
||
8 years ago
|
|
||
9 years ago
|
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
|
||
|
if (status == AVAuthorizationStatusDenied) {
|
||
8 years ago
|
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];
|
||
|
failureCallback();
|
||
|
}];
|
||
9 years ago
|
[alert addAction:openSettingsAction];
|
||
|
|
||
8 years ago
|
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton
|
||
9 years ago
|
style:UIAlertActionStyleCancel
|
||
8 years ago
|
handler:^(UIAlertAction *action) {
|
||
|
failureCallback();
|
||
|
}];
|
||
9 years ago
|
[alert addAction:dismissAction];
|
||
|
|
||
|
[self presentViewController:alert animated:YES completion:nil];
|
||
9 years ago
|
} else if (status == AVAuthorizationStatusAuthorized) {
|
||
8 years ago
|
successCallback();
|
||
9 years ago
|
} else if (status == AVAuthorizationStatusNotDetermined) {
|
||
8 years ago
|
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
|
||
|
completionHandler:^(BOOL granted) {
|
||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||
|
if (granted) {
|
||
8 years ago
|
successCallback();
|
||
8 years ago
|
} else {
|
||
|
failureCallback();
|
||
|
}
|
||
|
});
|
||
|
}];
|
||
9 years ago
|
} else {
|
||
|
DDLogError(@"Unknown AVAuthorizationStatus: %ld", (long)status);
|
||
8 years ago
|
failureCallback();
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
- (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) {
|
||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||
|
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);
|
||
|
}];
|
||
|
}
|
||
|
|
||
9 years ago
|
@end
|
||
|
|
||
|
NS_ASSUME_NONNULL_END
|