Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request microphone permission when recording video #808

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Source/Helpers/Permissions/YPPermissionCheckable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import UIKit
internal protocol YPPermissionCheckable {
func doAfterLibraryPermissionCheck(block: @escaping () -> Void)
func doAfterCameraPermissionCheck(block: @escaping () -> Void)
func doAfterMicrophonePermissionCheck(block: @escaping () -> Void)
func checkLibraryPermission()
func checkCameraPermission()
func checkMicrophonePermission()
}

internal extension YPPermissionCheckable where Self: UIViewController {
Expand All @@ -36,11 +38,25 @@ internal extension YPPermissionCheckable where Self: UIViewController {
}
}

func doAfterMicrophonePermissionCheck(block: @escaping () -> Void) {
YPPermissionManager.checkMicrophonePermissionAndAskIfNeeded(sourceVC: self) { hasPermission in
if hasPermission {
block()
} else {
ypLog("Not enough permissions.")
}
}
}

func checkLibraryPermission() {
YPPermissionManager.checkLibraryPermissionAndAskIfNeeded(sourceVC: self) { _ in }
}

func checkCameraPermission() {
YPPermissionManager.checkCameraPermissionAndAskIfNeeded(sourceVC: self) { _ in }
}

func checkMicrophonePermission() {
YPPermissionManager.checkMicrophonePermissionAndAskIfNeeded(sourceVC: self) { _ in }
}
}
45 changes: 45 additions & 0 deletions Source/Helpers/Permissions/YPPermissionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,49 @@ internal struct YPPermissionManager {
ypLog("Bug. Write to developers please.")
}
}

static func checkMicrophonePermissionAndAskIfNeeded(sourceVC: UIViewController,
completion: @escaping YPPermissionManagerCompletion) {
if #available(iOS 17, *) {
let permission = AVAudioApplication.shared.recordPermission

switch permission {
case .granted:
completion(true)
case .denied:
let alert = YPPermissionDeniedPopup.buildGoToSettingsAlert(cancelBlock: {
completion(false)
})
sourceVC.present(alert, animated: true, completion: nil)
case .undetermined:
AVAudioApplication.requestRecordPermission { granted in
DispatchQueue.main.async {
completion(granted)
}
}
@unknown default:
ypLog("Bug. Write to developers please.")
}
} else {
let permission = AVAudioSession.sharedInstance().recordPermission

switch permission {
case .granted:
completion(true)
case .denied:
let alert = YPPermissionDeniedPopup.buildGoToSettingsAlert(cancelBlock: {
completion(false)
})
sourceVC.present(alert, animated: true, completion: nil)
case .undetermined:
AVAudioSession.sharedInstance().requestRecordPermission { granted in
DispatchQueue.main.async {
completion(granted)
}
}
@unknown default:
ypLog("Bug. Write to developers please.")
}
}
}
}
4 changes: 3 additions & 1 deletion Source/Pages/Video/YPVideoCaptureVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ internal class YPVideoCaptureVC: UIViewController, YPPermissionCheckable {
@objc
func shotButtonTapped() {
doAfterCameraPermissionCheck { [weak self] in
self?.toggleRecording()
self?.doAfterMicrophonePermissionCheck {
self?.toggleRecording()
}
}
}

Expand Down