NCAskAuthorization.swift 5.0 KB

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