123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import UIKit
- class NCAskAuthorization: NSObject {
- @objc static let shared: NCAskAuthorization = {
- let instance = NCAskAuthorization()
- return instance
- }()
-
- func askAuthorizationAudioRecord(viewController: UIViewController?, completion: @escaping (_ hasPermission: Bool)->()) {
-
- 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: { action in
- UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
- completion(false)
- }))
- alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { action 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
- }
- }
-
- func askAuthorizationPhotoLibrary(viewController: UIViewController?, completion: @escaping (_ hasPermission: Bool)->()) {
-
- 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: { action in
- UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
- completion(false)
- }))
- alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { action in
- completion(false)
- }))
- DispatchQueue.main.async {
- viewController?.present(alert, animated: true, completion: nil)
- }
- break
- case PHAuthorizationStatus.notDetermined:
- PHPhotoLibrary.requestAuthorization { (allowed) in
- DispatchQueue.main.async {
- if allowed == PHAuthorizationStatus.authorized {
- completion(true)
- } else {
- completion(false)
- }
- }
- }
- break
- default:
- completion(false)
- break
- }
- }
-
- @objc func askAuthorizationLocationManager(completion: @escaping (_ hasFullPermissions: Bool)->()) {
-
- switch CLLocationManager.authorizationStatus() {
- case CLAuthorizationStatus.authorizedAlways:
- completion(true)
- break
-
- default:
- DispatchQueue.main.async {
- NCAutoUpload.shared.startSignificantChangeUpdates()
- }
- completion(false)
- break
- }
- }
- }
|