123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import UIKit
- class NCAskAuthorization: NSObject {
- @objc static let shared: NCAskAuthorization = {
- let instance = NCAskAuthorization()
- return instance
- }()
- private(set) var isRequesting = false
- func askAuthorizationAudioRecord(viewController: UIViewController?, completion: @escaping (_ hasPermission: Bool) -> Void) {
- switch AVAudioSession.sharedInstance().recordPermission {
- case AVAudioSession.RecordPermission.granted:
- completion(true)
- break
- case AVAudioSession.RecordPermission.denied:
- let alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_err_permission_microphone_", comment: ""), preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: NSLocalizedString("_open_settings_", comment: ""), style: .default, handler: { _ in
- UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
- completion(false)
- }))
- alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
- completion(false)
- }))
- DispatchQueue.main.async {
- viewController?.present(alert, animated: true, completion: nil)
- }
- break
- case AVAudioSession.RecordPermission.undetermined:
- AVAudioSession.sharedInstance().requestRecordPermission { allowed in
- DispatchQueue.main.async {
- if allowed {
- completion(true)
- } else {
- completion(false)
- }
- }
- }
- break
- default:
- completion(false)
- break
- }
- }
- @objc func askAuthorizationPhotoLibrary(viewController: UIViewController?, completion: @escaping (_ hasPermission: Bool) -> Void) {
- switch PHPhotoLibrary.authorizationStatus() {
- case PHAuthorizationStatus.authorized:
- completion(true)
- break
- case PHAuthorizationStatus.denied, PHAuthorizationStatus.limited, PHAuthorizationStatus.restricted:
- let alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_err_permission_photolibrary_", comment: ""), preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: NSLocalizedString("_open_settings_", comment: ""), style: .default, handler: { _ in
- UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
- completion(false)
- }))
- alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
- completion(false)
- }))
- DispatchQueue.main.async {
- viewController?.present(alert, animated: true, completion: nil)
- }
- break
- case PHAuthorizationStatus.notDetermined:
- isRequesting = true
- PHPhotoLibrary.requestAuthorization { allowed in
- self.isRequesting = false
- DispatchQueue.main.async {
- (UIApplication.shared.delegate as? AppDelegate)?.hidePrivacyProtectionWindow()
- }
- DispatchQueue.main.async {
- if allowed == PHAuthorizationStatus.authorized {
- completion(true)
- } else {
- completion(false)
- }
- }
- }
- break
- default:
- completion(false)
- break
- }
- }
- }
|