From 47f9984d332216c813459f4ca9a59b7f1c127dba Mon Sep 17 00:00:00 2001 From: Ryan ZHAO <> Date: Wed, 8 Jan 2025 16:56:46 +1100 Subject: [PATCH] further refactor to use latest API --- .../ConversationVC+Interaction.swift | 2 +- .../Message Cells/CallMessageCell.swift | 4 +- Session/Utilities/Permissions.swift | 80 +++++++++++-------- 3 files changed, 50 insertions(+), 36 deletions(-) diff --git a/Session/Conversations/ConversationVC+Interaction.swift b/Session/Conversations/ConversationVC+Interaction.swift index e39f78aff..d57ca2b39 100644 --- a/Session/Conversations/ConversationVC+Interaction.swift +++ b/Session/Conversations/ConversationVC+Interaction.swift @@ -323,7 +323,7 @@ extension ConversationVC: Permissions.requestMicrophonePermissionIfNeeded() - if AVAudioSession.sharedInstance().recordPermission != .granted { + if !Permissions.hasMicrophonePermission { SNLog("Proceeding without microphone access. Any recorded video will be silent.") } diff --git a/Session/Conversations/Message Cells/CallMessageCell.swift b/Session/Conversations/Message Cells/CallMessageCell.swift index 7476d2f9d..31a639dc3 100644 --- a/Session/Conversations/Message Cells/CallMessageCell.swift +++ b/Session/Conversations/Message Cells/CallMessageCell.swift @@ -169,7 +169,7 @@ final class CallMessageCell: MessageCell { !Storage.shared[.areCallsEnabled] ) || ( messageInfo.state == .permissionDeniedMicrophone && - AVAudioSession.sharedInstance().recordPermission != .granted + !Permissions.hasMicrophonePermission ) ) infoImageViewWidthConstraint.constant = (shouldShowInfoIcon ? CallMessageCell.iconSize : 0) @@ -230,7 +230,7 @@ final class CallMessageCell: MessageCell { !Storage.shared[.areCallsEnabled] ) || ( messageInfo.state == .permissionDeniedMicrophone && - AVAudioSession.sharedInstance().recordPermission != .granted + !Permissions.hasMicrophonePermission ) else { return } diff --git a/Session/Utilities/Permissions.swift b/Session/Utilities/Permissions.swift index 692c2d8e1..3b221af93 100644 --- a/Session/Utilities/Permissions.swift +++ b/Session/Utilities/Permissions.swift @@ -57,40 +57,54 @@ extension Permissions { presentingViewController: UIViewController? = nil, onNotGranted: (() -> Void)? = nil ) { - switch AVAudioSession.sharedInstance().recordPermission { - case .granted: break - case .denied: - guard - Singleton.hasAppContext, - let presentingViewController: UIViewController = (presentingViewController ?? Singleton.appContext.frontmostViewController) - else { return } - onNotGranted?() - - let confirmationModal: ConfirmationModal = ConfirmationModal( - info: ConfirmationModal.Info( - title: "permissionsRequired".localized(), - body: .text( - "permissionsMicrophoneAccessRequiredIos" - .put(key: "app_name", value: Constants.app_name) - .localized() - ), - confirmTitle: "sessionSettings".localized(), - dismissOnConfirm: false, - onConfirm: { [weak presentingViewController] _ in - presentingViewController?.dismiss(animated: true, completion: { - UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!) - }) - }, - afterClosed: { onNotGranted?() } - ) + let handlePermissionDenied: () -> Void = { + guard + Singleton.hasAppContext, + let presentingViewController: UIViewController = (presentingViewController ?? Singleton.appContext.frontmostViewController) + else { return } + onNotGranted?() + + let confirmationModal: ConfirmationModal = ConfirmationModal( + info: ConfirmationModal.Info( + title: "permissionsRequired".localized(), + body: .text( + "permissionsMicrophoneAccessRequiredIos" + .put(key: "app_name", value: Constants.app_name) + .localized() + ), + confirmTitle: "sessionSettings".localized(), + dismissOnConfirm: false, + onConfirm: { [weak presentingViewController] _ in + presentingViewController?.dismiss(animated: true, completion: { + UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!) + }) + }, + afterClosed: { onNotGranted?() } ) - presentingViewController.present(confirmationModal, animated: true, completion: nil) - - case .undetermined: - onNotGranted?() - AVAudioSession.sharedInstance().requestRecordPermission { _ in } - - default: break + ) + presentingViewController.present(confirmationModal, animated: true, completion: nil) + } + + if #available(iOS 17.0, *) { + switch AVAudioApplication.shared.recordPermission { + case .granted: break + case .denied: handlePermissionDenied() + case .undetermined: + onNotGranted?() + AVAudioSession.sharedInstance().requestRecordPermission { _ in } + + default: break + } + } else { + switch AVAudioSession.sharedInstance().recordPermission { + case .granted: break + case .denied: handlePermissionDenied() + case .undetermined: + onNotGranted?() + AVAudioSession.sharedInstance().requestRecordPermission { _ in } + + default: break + } } }