Created
August 7, 2020 14:58
-
-
Save alexey1312/57160fca8ae1e1a10268db019a06757c to your computer and use it in GitHub Desktop.
Request to use microphone + go to application settings if access was not allowed earlier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (void) checkMicrophoneAccess { | |
| switch ([[AVAudioSession sharedInstance] recordPermission]) { | |
| case AVAudioSessionRecordPermissionGranted: | |
| [self addAudioFromMicrophone]; | |
| NSLog(@"πΉ Mic enabled π"); | |
| break; | |
| case AVAudioSessionRecordPermissionDenied: | |
| NSLog(@"πΉ Mic does not have permission π"); | |
| [self presentMicrophoneSettings]; | |
| break; | |
| case AVAudioSessionRecordPermissionUndetermined: | |
| [[AVAudioSession sharedInstance] requestRecordPermission: ^(BOOL granted) { | |
| NSLog(@"Mic enabled %@", granted == YES ? @"YES" : @"NO"); | |
| }]; | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| - (void) presentMicrophoneSettings { | |
| UIAlertController * alert = [UIAlertController | |
| alertControllerWithTitle: NSLocalizedString(@"common_alert_title_warning", nil) | |
| message: NSLocalizedString(@"toast_error_permission_mic", nil) | |
| preferredStyle: UIAlertControllerStyleAlert]; | |
| UIAlertAction* settingsButton = [UIAlertAction | |
| actionWithTitle: NSLocalizedString(@"application_settings_title", nil) | |
| style: UIAlertActionStyleDefault | |
| handler:^(UIAlertAction * _Nonnull action) { | |
| NSURL * url = [NSURL URLWithString: UIApplicationOpenSettingsURLString]; | |
| if ([[UIApplication sharedApplication] canOpenURL: url]) { | |
| [[UIApplication sharedApplication] openURL: url options: @{} completionHandler: nil]; | |
| } | |
| }]; | |
| UIAlertAction* cancelButton = [UIAlertAction | |
| actionWithTitle: NSLocalizedString(@"common_button_cancel", nil) | |
| style: UIAlertActionStyleDestructive | |
| handler: nil]; | |
| [alert addAction: settingsButton]; | |
| [alert addAction: cancelButton]; | |
| [UIApplication.sharedApplication.keyWindow.rootViewController presentViewController: alert animated: YES completion: nil]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func checkMicrophoneAccess() { | |
| switch AVAudioSession.sharedInstance().recordPermission { | |
| case .granted: | |
| print("πΉ Mic enabled π") | |
| case .denied: | |
| print("πΉ Mic does not have permission π") | |
| presentMicrophoneSettings() | |
| case .undetermined: | |
| AVAudioSession.sharedInstance().requestRecordPermission({ granted in | |
| print("Mic enabled \(granted == true ? "YES" : "NO")") | |
| }) | |
| default: | |
| break | |
| } | |
| } | |
| func presentMicrophoneSettings() { | |
| let alertController = UIAlertController(title: NSLocalizedString("common_alert_title_warning", comment: ""), | |
| message: NSLocalizedString("toast_error_permission_camera", comment: ""), | |
| preferredStyle: .alert) | |
| alertController.addAction(UIAlertAction(title: NSLocalizedString("application_settings_title", comment: ""), | |
| style: .default) { _ in | |
| guard let url = URL(string: UIApplication.openSettingsURLString) else { return } | |
| UIApplication.shared.open(url, options: [:], completionHandler: nil) | |
| }) | |
| alertController.addAction(UIAlertAction(title: NSLocalizedString("common_button_cancel", comment: ""), | |
| style: .destructive)) | |
| present(alertController, animated: true) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment