Skip to content

Instantly share code, notes, and snippets.

@alexey1312
Created August 7, 2020 14:58
Show Gist options
  • Save alexey1312/57160fca8ae1e1a10268db019a06757c to your computer and use it in GitHub Desktop.
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
- (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];
}
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