From c152c1c83ae4fbe94ddd9682088130ec2b701ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Pawlak?= Date: Fri, 4 Nov 2016 23:41:37 +0100 Subject: [PATCH] asking for camera permissions #1363 --- Signal.xcodeproj/project.pbxproj | 6 +++ Signal/src/Storyboard/Main.storyboard | 9 ++-- .../util/UIViewController+CameraPermissions.h | 16 +++++++ .../util/UIViewController+CameraPermissions.m | 46 +++++++++++++++++++ .../FingerprintViewController.m | 31 +++++++------ .../view controllers/MessagesViewController.m | 21 ++++----- .../OWSLinkedDevicesTableViewController.m | 10 ++++ 7 files changed, 110 insertions(+), 29 deletions(-) create mode 100644 Signal/src/util/UIViewController+CameraPermissions.h create mode 100644 Signal/src/util/UIViewController+CameraPermissions.m diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index bfa2608a3..86fe19349 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -473,6 +473,7 @@ E197B62418BBF5BB00F073E5 /* SoundPlayer.m in Sources */ = {isa = PBXBuildFile; fileRef = E197B62318BBF5BB00F073E5 /* SoundPlayer.m */; }; E197B62718BBF63B00F073E5 /* SoundBoard.m in Sources */ = {isa = PBXBuildFile; fileRef = E197B62618BBF63B00F073E5 /* SoundBoard.m */; }; E1CD329618BCFF9900B1A496 /* SoundInstance.m in Sources */ = {isa = PBXBuildFile; fileRef = E1CD329518BCFF9900B1A496 /* SoundInstance.m */; }; + EF764C351DB67CC5000D9A87 /* UIViewController+CameraPermissions.m in Sources */ = {isa = PBXBuildFile; fileRef = EF764C341DB67CC5000D9A87 /* UIViewController+CameraPermissions.m */; }; FC31962A1A067D8F0094C78E /* MessageComposeTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = FC3196291A067D8F0094C78E /* MessageComposeTableViewController.m */; }; FC31962D1A06A2190094C78E /* FingerprintViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = FC31962C1A06A2190094C78E /* FingerprintViewController.m */; }; FC3196301A0814130094C78E /* SettingsTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = FC31962F1A0814130094C78E /* SettingsTableViewController.m */; }; @@ -1090,6 +1091,8 @@ E1CD329418BCFF9900B1A496 /* SoundInstance.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SoundInstance.h; sourceTree = ""; }; E1CD329518BCFF9900B1A496 /* SoundInstance.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SoundInstance.m; sourceTree = ""; }; E85DB184824BA9DC302EC8B3 /* Pods-SignalTests.app store release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SignalTests.app store release.xcconfig"; path = "Pods/Target Support Files/Pods-SignalTests/Pods-SignalTests.app store release.xcconfig"; sourceTree = ""; }; + EF764C331DB67CC5000D9A87 /* UIViewController+CameraPermissions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIViewController+CameraPermissions.h"; path = "util/UIViewController+CameraPermissions.h"; sourceTree = ""; }; + EF764C341DB67CC5000D9A87 /* UIViewController+CameraPermissions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIViewController+CameraPermissions.m"; path = "util/UIViewController+CameraPermissions.m"; sourceTree = ""; }; FC3196281A067D8F0094C78E /* MessageComposeTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MessageComposeTableViewController.h; sourceTree = ""; }; FC3196291A067D8F0094C78E /* MessageComposeTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MessageComposeTableViewController.m; sourceTree = ""; }; FC31962B1A06A2190094C78E /* FingerprintViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = FingerprintViewController.h; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; @@ -2412,6 +2415,8 @@ FCFA64B61A24F6730007FB87 /* UIFont+OWS.m */, B68112E81A4D9EC400BA82FF /* UIImage+normalizeImage.h */, B68112E91A4D9EC400BA82FF /* UIImage+normalizeImage.m */, + EF764C331DB67CC5000D9A87 /* UIViewController+CameraPermissions.h */, + EF764C341DB67CC5000D9A87 /* UIViewController+CameraPermissions.m */, ); name = "UI Categories"; path = ..; @@ -2820,6 +2825,7 @@ 76EB05A818170B33006006FC /* RtpSocket.m in Sources */, E197B61818BBEC1A00F073E5 /* RemoteIOAudio.m in Sources */, B67ADDC41989FF8700E1A773 /* RPServerRequestsManager.m in Sources */, + EF764C351DB67CC5000D9A87 /* UIViewController+CameraPermissions.m in Sources */, 76EB059418170B33006006FC /* HttpManager.m in Sources */, 45CD81EF1DC030E7004C9430 /* AccountManager.swift in Sources */, 76EB05EC18170B33006006FC /* CallState.m in Sources */, diff --git a/Signal/src/Storyboard/Main.storyboard b/Signal/src/Storyboard/Main.storyboard index 9f6ebc085..0900049f3 100644 --- a/Signal/src/Storyboard/Main.storyboard +++ b/Signal/src/Storyboard/Main.storyboard @@ -912,9 +912,6 @@ - - - @@ -923,10 +920,13 @@ + + + - + @@ -1519,6 +1519,7 @@ + diff --git a/Signal/src/util/UIViewController+CameraPermissions.h b/Signal/src/util/UIViewController+CameraPermissions.h new file mode 100644 index 000000000..bd1b6d121 --- /dev/null +++ b/Signal/src/util/UIViewController+CameraPermissions.h @@ -0,0 +1,16 @@ +// +// UIViewController+CameraPermissions.h +// Signal +// +// Created by Jarosław Pawlak on 18.10.2016. +// Copyright © 2016 Open Whisper Systems. All rights reserved. +// + +#import +NS_ASSUME_NONNULL_BEGIN +@interface UIViewController (CameraPermissions) + +-(void)ows_askForCameraPermissions:(void(^)())permissionsGrantedCallback; + +@end +NS_ASSUME_NONNULL_END diff --git a/Signal/src/util/UIViewController+CameraPermissions.m b/Signal/src/util/UIViewController+CameraPermissions.m new file mode 100644 index 000000000..575613e9c --- /dev/null +++ b/Signal/src/util/UIViewController+CameraPermissions.m @@ -0,0 +1,46 @@ +// +// UIViewController+CameraPermissions.m +// Signal +// +// Created by Jarosław Pawlak on 18.10.2016. +// Copyright © 2016 Open Whisper Systems. All rights reserved. +// +#import "UIUtil.h" +#import "UIViewController+CameraPermissions.h" +#import + +NS_ASSUME_NONNULL_BEGIN + +@implementation UIViewController (CameraPermissions) + +-(void)ows_askForCameraPermissions:(void(^)())permissionsGrantedCallback +{ + if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { + DDLogError(@"Camera ImagePicker source not available"); + return; + } + AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; + if (status == AVAuthorizationStatusDenied) { + UIAlertController* alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"CAMERA_PERMISSION_TITLE",nil) message:NSLocalizedString(@"CAMERA_PERMISSION_MESSAGE",nil) preferredStyle:UIAlertControllerStyleAlert]; + + [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"CAMERA_PERMISSION_PROCEED",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; + }]]; + [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"CAMERA_PERMISSION_CANCEL",nil) style:UIAlertActionStyleCancel handler:nil]]; + [self presentViewController:alert animated:YES completion:[UIUtil modalCompletionBlock]]; + } else if (status == AVAuthorizationStatusAuthorized) { + permissionsGrantedCallback(); + } else if (status == AVAuthorizationStatusNotDetermined) { + [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { + if (granted) { + permissionsGrantedCallback(); + } + }]; + } else { + DDLogError(@"Unknown AVAuthorizationStatus: %ld", (long)status); + } +} + +@end + +NS_ASSUME_NONNULL_END diff --git a/Signal/src/view controllers/FingerprintViewController.m b/Signal/src/view controllers/FingerprintViewController.m index a35bd9e8f..257465a43 100644 --- a/Signal/src/view controllers/FingerprintViewController.m +++ b/Signal/src/view controllers/FingerprintViewController.m @@ -10,6 +10,7 @@ #import "DJWActionSheet+OWS.h" #import "Environment.h" #import "OWSConversationSettingsTableViewController.h" +#import "UIViewController+CameraPermissions.h" #import "Signal-Swift.h" #import #import @@ -137,20 +138,22 @@ NS_ASSUME_NONNULL_BEGIN - (void)showScanner { - DDLogInfo(@"%@ Showing Scanner", self.tag); - self.qrScanningView.hidden = NO; - self.scanningInstructions.hidden = NO; - [UIView animateWithDuration:0.4 - delay:0.0 - options:UIViewAnimationOptionCurveEaseInOut - animations:^{ - self.scanningContainer.frame = self.qrContainer.frame; - self.qrContainer.frame = self.instructionsContainer.frame; - self.instructionsContainer.alpha = 0.0f; - } - completion:nil]; - - [self.qrScanningController startCapture]; + [self ows_askForCameraPermissions:^{ + DDLogInfo(@"%@ Showing Scanner", self.tag); + self.qrScanningView.hidden = NO; + self.scanningInstructions.hidden = NO; + [UIView animateWithDuration:0.4 + delay:0.0 + options:UIViewAnimationOptionCurveEaseInOut + animations:^{ + self.scanningContainer.frame = self.qrContainer.frame; + self.qrContainer.frame = self.instructionsContainer.frame; + self.instructionsContainer.alpha = 0.0f; + } + completion:nil]; + + [self.qrScanningController startCapture]; + }]; } // pragma mark - OWSQRScannerDelegate diff --git a/Signal/src/view controllers/MessagesViewController.m b/Signal/src/view controllers/MessagesViewController.m index bb9f660b9..9cdd1c794 100644 --- a/Signal/src/view controllers/MessagesViewController.m +++ b/Signal/src/view controllers/MessagesViewController.m @@ -41,6 +41,7 @@ #import "TSInvalidIdentityKeyErrorMessage.h" #import "UIFont+OWS.h" #import "UIUtil.h" +#import "UIViewController+CameraPermissions.h" #import #import #import @@ -64,6 +65,7 @@ #import #import + @import Photos; #define kYapDatabaseRangeLength 50 @@ -1560,19 +1562,16 @@ typedef enum : NSUInteger { */ - (void)takePictureOrVideo { - if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { - DDLogError(@"Camera ImagePicker source not available"); - return; - } + [self ows_askForCameraPermissions:^{ + UIImagePickerController *picker = [[UIImagePickerController alloc] init]; + picker.sourceType = UIImagePickerControllerSourceTypeCamera; + picker.mediaTypes = @[ (__bridge NSString *)kUTTypeImage, (__bridge NSString *)kUTTypeMovie ]; + picker.allowsEditing = NO; + picker.delegate = self; + [self presentViewController:picker animated:YES completion:[UIUtil modalCompletionBlock]]; - UIImagePickerController *picker = [[UIImagePickerController alloc] init]; - picker.sourceType = UIImagePickerControllerSourceTypeCamera; - picker.mediaTypes = @[ (__bridge NSString *)kUTTypeImage, (__bridge NSString *)kUTTypeMovie ]; - picker.allowsEditing = NO; - picker.delegate = self; - [self presentViewController:picker animated:YES completion:[UIUtil modalCompletionBlock]]; + }]; } - - (void)chooseFromLibrary { if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { DDLogError(@"PhotoLibrary ImagePicker source not available"); diff --git a/Signal/src/view controllers/OWSLinkedDevicesTableViewController.m b/Signal/src/view controllers/OWSLinkedDevicesTableViewController.m index 61fbca5d0..129317589 100644 --- a/Signal/src/view controllers/OWSLinkedDevicesTableViewController.m +++ b/Signal/src/view controllers/OWSLinkedDevicesTableViewController.m @@ -3,6 +3,7 @@ #import "OWSLinkedDevicesTableViewController.h" #import "OWSDeviceTableViewCell.h" #import "OWSLinkDeviceViewController.h" +#import "UIViewController+CameraPermissions.h" #import #import #import @@ -248,6 +249,15 @@ int const OWSLinkedDevicesTableViewControllerSectionAddDevice = 1; } } +-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath +{ + if (indexPath.section == OWSLinkedDevicesTableViewControllerSectionAddDevice) + { + [self ows_askForCameraPermissions:^{ + [self performSegueWithIdentifier:@"LinkDeviceSegue" sender:self]; + }]; + } +} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == OWSLinkedDevicesTableViewControllerSectionAddDevice) {