session-ios/Session/Utilities/UIViewController+Permissions.m

176 lines
7.0 KiB
Mathematica
Raw Normal View History

2016-11-04 23:41:37 +01:00
//
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
2016-11-04 23:41:37 +01:00
//
2017-05-05 18:39:21 +02:00
#import "UIViewController+Permissions.h"
2019-05-02 23:58:48 +02:00
#import "Session-Swift.h"
2017-11-08 20:04:51 +01:00
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>
2020-11-12 00:41:45 +01:00
#import <SignalCoreKit/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
{
OWSLogVerbose(@"[%@] ows_askForCameraPermissions", NSStringFromClass(self.class));
// Ensure callback is invoked on main thread.
void (^callback)(BOOL) = ^(BOOL granted) {
DispatchMainThreadSafe(^{
callbackParam(granted);
});
};
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
OWSLogError(@"Skipping camera permissions request when app is in background.");
callback(NO);
return;
}
2016-11-04 23:41:37 +01:00
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
OWSLogError(@"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
2020-01-09 23:52:30 +01:00
alertControllerWithTitle:NSLocalizedString(@"Session needs camera access to scan QR codes.", @"")
2019-08-23 07:12:08 +02:00
message:NSLocalizedString(@"You can enable camera access in your device settings.", @"")
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *openSettingsAction =
[UIAlertAction actionWithTitle:CommonStrings.openSettingsButton
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 presentAlert:alert];
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
2017-11-09 09:14:58 +01:00
completionHandler:callback];
2016-11-04 23:41:37 +01:00
} else {
OWSLogError(@"Unknown AVAuthorizationStatus: %ld", (long)status);
callback(NO);
2016-11-04 23:41:37 +01:00
}
}
- (void)ows_askForMediaLibraryPermissions:(void (^)(BOOL granted))callbackParam
{
OWSLogVerbose(@"[%@] ows_askForMediaLibraryPermissions", NSStringFromClass(self.class));
// Ensure callback is invoked on main thread.
void (^completionCallback)(BOOL) = ^(BOOL granted) {
DispatchMainThreadSafe(^{
callbackParam(granted);
});
};
void (^presentSettingsDialog)(void) = ^(void) {
DispatchMainThreadSafe(^{
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:NSLocalizedString(@"MISSING_MEDIA_LIBRARY_PERMISSION_TITLE",
@"Alert title when user has previously denied media library access")
message:NSLocalizedString(@"MISSING_MEDIA_LIBRARY_PERMISSION_MESSAGE",
@"Alert body when user has previously denied media library access")
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *openSettingsAction =
[UIAlertAction actionWithTitle:CommonStrings.openSettingsButton
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
[[UIApplication sharedApplication] openSystemSettings];
completionCallback(NO);
}];
[alert addAction:openSettingsAction];
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
completionCallback(NO);
}];
[alert addAction:dismissAction];
[self presentAlert:alert];
});
};
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
OWSLogError(@"Skipping media library permissions request when app is in background.");
completionCallback(NO);
return;
}
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
OWSLogError(@"PhotoLibrary ImagePicker source not available");
completionCallback(NO);
}
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
switch (status) {
case PHAuthorizationStatusAuthorized: {
completionCallback(YES);
return;
}
case PHAuthorizationStatusDenied: {
presentSettingsDialog();
return;
}
case PHAuthorizationStatusNotDetermined: {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus newStatus) {
if (newStatus == PHAuthorizationStatusAuthorized) {
completionCallback(YES);
} else {
presentSettingsDialog();
}
}];
return;
}
case PHAuthorizationStatusRestricted: {
// when does this happen?
2018-08-27 16:29:51 +02:00
OWSFailDebug(@"PHAuthorizationStatusRestricted");
return;
}
}
}
- (void)ows_askForMicrophonePermissions:(void (^)(BOOL granted))callbackParam
{
OWSLogVerbose(@"[%@] ows_askForMicrophonePermissions", NSStringFromClass(self.class));
// Ensure callback is invoked on main thread.
void (^callback)(BOOL) = ^(BOOL granted) {
DispatchMainThreadSafe(^{
callbackParam(granted);
});
};
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
OWSLogError(@"Skipping microphone permissions request when app is in background.");
callback(NO);
return;
}
2017-11-09 09:14:58 +01:00
[[AVAudioSession sharedInstance] requestRecordPermission:callback];
}
2016-11-04 23:41:37 +01:00
@end
NS_ASSUME_NONNULL_END