NCAskAuthorization.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // NCAskAuthorization.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 27/01/21.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. class NCAskAuthorization: NSObject {
  25. @objc static let shared: NCAskAuthorization = {
  26. let instance = NCAskAuthorization()
  27. return instance
  28. }()
  29. private(set) var isRequesting = false
  30. func askAuthorizationAudioRecord(viewController: UIViewController?, completion: @escaping (_ hasPermission: Bool) -> Void) {
  31. switch AVAudioSession.sharedInstance().recordPermission {
  32. case AVAudioSession.RecordPermission.granted:
  33. completion(true)
  34. break
  35. case AVAudioSession.RecordPermission.denied:
  36. let alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_err_permission_microphone_", comment: ""), preferredStyle: .alert)
  37. alert.addAction(UIAlertAction(title: NSLocalizedString("_open_settings_", comment: ""), style: .default, handler: { _ in
  38. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
  39. completion(false)
  40. }))
  41. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
  42. completion(false)
  43. }))
  44. DispatchQueue.main.async {
  45. viewController?.present(alert, animated: true, completion: nil)
  46. }
  47. break
  48. case AVAudioSession.RecordPermission.undetermined:
  49. AVAudioSession.sharedInstance().requestRecordPermission { allowed in
  50. DispatchQueue.main.async {
  51. if allowed {
  52. completion(true)
  53. } else {
  54. completion(false)
  55. }
  56. }
  57. }
  58. break
  59. default:
  60. completion(false)
  61. break
  62. }
  63. }
  64. @objc func askAuthorizationPhotoLibrary(viewController: UIViewController?, completion: @escaping (_ hasPermission: Bool) -> Void) {
  65. switch PHPhotoLibrary.authorizationStatus() {
  66. case PHAuthorizationStatus.authorized:
  67. completion(true)
  68. break
  69. case PHAuthorizationStatus.denied, PHAuthorizationStatus.limited, PHAuthorizationStatus.restricted:
  70. let alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_err_permission_photolibrary_", comment: ""), preferredStyle: .alert)
  71. alert.addAction(UIAlertAction(title: NSLocalizedString("_open_settings_", comment: ""), style: .default, handler: { _ in
  72. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
  73. completion(false)
  74. }))
  75. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
  76. completion(false)
  77. }))
  78. DispatchQueue.main.async {
  79. viewController?.present(alert, animated: true, completion: nil)
  80. }
  81. break
  82. case PHAuthorizationStatus.notDetermined:
  83. isRequesting = true
  84. PHPhotoLibrary.requestAuthorization { allowed in
  85. self.isRequesting = false
  86. DispatchQueue.main.async {
  87. (UIApplication.shared.delegate as? AppDelegate)?.hidePrivacyProtectionWindow()
  88. }
  89. DispatchQueue.main.async {
  90. if allowed == PHAuthorizationStatus.authorized {
  91. completion(true)
  92. } else {
  93. completion(false)
  94. }
  95. }
  96. }
  97. break
  98. default:
  99. completion(false)
  100. break
  101. }
  102. }
  103. @objc func askAuthorizationLocationManager(completion: @escaping (_ hasFullPermissions: Bool) -> Void) {
  104. switch CLLocationManager.authorizationStatus() {
  105. case CLAuthorizationStatus.authorizedAlways:
  106. completion(true)
  107. break
  108. /*
  109. case CLAuthorizationStatus.authorizedWhenInUse, CLAuthorizationStatus.denied, CLAuthorizationStatus.restricted:
  110. DispatchQueue.main.async {
  111. NCAutoUpload.shared.startSignificantChangeUpdates()
  112. }
  113. completion(false)
  114. break
  115. case CLAuthorizationStatus.notDetermined:
  116. DispatchQueue.main.async {
  117. NCAutoUpload.shared.startSignificantChangeUpdates()
  118. }
  119. completion(false)
  120. break
  121. */
  122. default:
  123. DispatchQueue.main.async {
  124. NCAutoUpload.shared.startSignificantChangeUpdates()
  125. }
  126. completion(false)
  127. break
  128. }
  129. }
  130. }