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

102 lines
3.9 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
#import "Signal-Swift.h"
2017-11-08 20:04:51 +01:00
#import "UIUtil.h"
#import "UIViewController+Permissions.h"
2017-11-08 20:04:51 +01:00
#import <AVFoundation/AVFoundation.h>
#import <SignalServiceKit/Threading.h>
2016-11-04 23:41:37 +01:00
NS_ASSUME_NONNULL_BEGIN
@implementation UIViewController (Permissions)
2016-11-04 23:41:37 +01:00
- (void)ows_askForCameraPermissions:(void (^)(BOOL granted))callbackParam
{
DDLogVerbose(@"[%@] ows_askForCameraPermissions", NSStringFromClass(self.class));
// Ensure callback is invoked on main thread.
void (^callback)(BOOL) = ^(BOOL granted) {
DispatchMainThreadSafe(^{
callbackParam(granted);
});
};
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
DDLogError(@"Skipping camera permissions request when app is not active.");
callback(NO);
return;
}
2016-11-04 23:41:37 +01:00
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
DDLogError(@"Camera ImagePicker source not available");
callback(NO);
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) {
[[UIApplication sharedApplication] openSystemSettings];
callback(NO);
}];
[alert addAction:openSettingsAction];
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
callback(NO);
}];
[alert addAction:dismissAction];
[self presentViewController:alert animated:YES completion:nil];
2016-11-04 23:41:37 +01:00
} else if (status == AVAuthorizationStatusAuthorized) {
callback(YES);
2016-11-04 23:41:37 +01:00
} else if (status == AVAuthorizationStatusNotDetermined) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
completionHandler:^(BOOL granted) {
callback(granted);
}];
2016-11-04 23:41:37 +01:00
} else {
DDLogError(@"Unknown AVAuthorizationStatus: %ld", (long)status);
callback(NO);
2016-11-04 23:41:37 +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) {
DispatchMainThreadSafe(^{
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