NCAskAuthorization.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #if !EXTENSION
  39. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
  40. #endif
  41. completion(false)
  42. }))
  43. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
  44. completion(false)
  45. }))
  46. DispatchQueue.main.async {
  47. viewController?.present(alert, animated: true, completion: nil)
  48. }
  49. break
  50. case AVAudioSession.RecordPermission.undetermined:
  51. AVAudioSession.sharedInstance().requestRecordPermission { allowed in
  52. DispatchQueue.main.async {
  53. if allowed {
  54. completion(true)
  55. } else {
  56. completion(false)
  57. }
  58. }
  59. }
  60. break
  61. default:
  62. completion(false)
  63. break
  64. }
  65. }
  66. @objc func askAuthorizationPhotoLibrary(viewController: UIViewController?, completion: @escaping (_ hasPermission: Bool) -> Void) {
  67. switch PHPhotoLibrary.authorizationStatus() {
  68. case PHAuthorizationStatus.authorized:
  69. completion(true)
  70. break
  71. case PHAuthorizationStatus.denied, PHAuthorizationStatus.limited, PHAuthorizationStatus.restricted:
  72. let alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_err_permission_photolibrary_", comment: ""), preferredStyle: .alert)
  73. alert.addAction(UIAlertAction(title: NSLocalizedString("_open_settings_", comment: ""), style: .default, handler: { _ in
  74. #if !EXTENSION
  75. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
  76. #endif
  77. completion(false)
  78. }))
  79. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
  80. completion(false)
  81. }))
  82. DispatchQueue.main.async {
  83. viewController?.present(alert, animated: true, completion: nil)
  84. }
  85. break
  86. case PHAuthorizationStatus.notDetermined:
  87. isRequesting = true
  88. PHPhotoLibrary.requestAuthorization { allowed in
  89. self.isRequesting = false
  90. #if !EXTENSION
  91. DispatchQueue.main.async { (UIApplication.shared.delegate as? AppDelegate)?.hidePrivacyProtectionWindow() }
  92. #endif
  93. DispatchQueue.main.async {
  94. if allowed == PHAuthorizationStatus.authorized {
  95. completion(true)
  96. } else {
  97. completion(false)
  98. }
  99. }
  100. }
  101. break
  102. default:
  103. completion(false)
  104. break
  105. }
  106. }
  107. #if !EXTENSION
  108. func checkBackgroundRefreshStatus() {
  109. switch UIApplication.shared.backgroundRefreshStatus {
  110. case .available:
  111. print("Background fetch is enabled")
  112. case .denied:
  113. print("Background fetch is explicitly disabled")
  114. // Redirect user to Settings page only once; Respect user's choice is important
  115. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
  116. case .restricted:
  117. // Should not redirect user to Settings since he / she cannot toggle the settings
  118. print("Background fetch is restricted, e.g. under parental control")
  119. default:
  120. print("Unknown property")
  121. }
  122. }
  123. #endif
  124. }