NCAskAuthorization.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Foundation
  24. class NCAskAuthorization: NSObject {
  25. @objc static let shared: NCAskAuthorization = {
  26. let instance = NCAskAuthorization()
  27. return instance
  28. }()
  29. func askAuthorizationAudioRecord(viewController: UIViewController, completion: @escaping (_ hasPermissions: Bool)->()) {
  30. switch AVAudioSession.sharedInstance().recordPermission {
  31. case AVAudioSession.RecordPermission.granted:
  32. completion(true)
  33. break
  34. case AVAudioSession.RecordPermission.denied:
  35. let alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_err_permission_microphone_", comment: ""), preferredStyle: .alert)
  36. alert.addAction(UIAlertAction(title: NSLocalizedString("_open_settings_", comment: ""), style: .default, handler: { action in
  37. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
  38. completion(false)
  39. }))
  40. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { action in
  41. completion(false)
  42. }))
  43. viewController.present(alert, animated: true, completion: nil)
  44. break
  45. case AVAudioSession.RecordPermission.undetermined:
  46. AVAudioSession.sharedInstance().requestRecordPermission { (allowed) in
  47. DispatchQueue.main.async {
  48. if allowed {
  49. completion(true)
  50. } else {
  51. completion(false)
  52. }
  53. }
  54. }
  55. break
  56. default:
  57. completion(false)
  58. break
  59. }
  60. }
  61. func askAuthorizationPhotoLibrary(viewController: UIViewController, completion: @escaping (_ hasPermissions: Bool)->()) {
  62. switch PHPhotoLibrary.authorizationStatus() {
  63. case PHAuthorizationStatus.authorized:
  64. completion(true)
  65. break
  66. case PHAuthorizationStatus.denied, PHAuthorizationStatus.limited, PHAuthorizationStatus.restricted:
  67. let alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_err_permission_photolibrary_", comment: ""), preferredStyle: .alert)
  68. alert.addAction(UIAlertAction(title: NSLocalizedString("_open_settings_", comment: ""), style: .default, handler: { action in
  69. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
  70. completion(false)
  71. }))
  72. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { action in
  73. completion(false)
  74. }))
  75. viewController.present(alert, animated: true, completion: nil)
  76. break
  77. case PHAuthorizationStatus.notDetermined:
  78. PHPhotoLibrary.requestAuthorization { (allowed) in
  79. DispatchQueue.main.async {
  80. if allowed == PHAuthorizationStatus.authorized {
  81. completion(true)
  82. } else {
  83. completion(false)
  84. }
  85. }
  86. }
  87. break
  88. default:
  89. completion(false)
  90. break
  91. }
  92. }
  93. func askAuthorizationLocationManager(viewController: UIViewController?, completion: @escaping (_ hasPermissions: Bool)->()) {
  94. switch CLLocationManager.authorizationStatus() {
  95. case CLAuthorizationStatus.authorizedAlways:
  96. completion(true)
  97. break
  98. case CLAuthorizationStatus.authorizedWhenInUse, CLAuthorizationStatus.denied, CLAuthorizationStatus.restricted:
  99. let alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_err_permission_locationmanager_", comment: ""), preferredStyle: .alert)
  100. alert.addAction(UIAlertAction(title: NSLocalizedString("_open_settings_", comment: ""), style: .default, handler: { action in
  101. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
  102. completion(false)
  103. }))
  104. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { action in
  105. completion(false)
  106. }))
  107. viewController?.present(alert, animated: true, completion: nil)
  108. break
  109. case CLAuthorizationStatus.notDetermined:
  110. NCManageLocation.shared.startSignificantChangeUpdates()
  111. completion(false)
  112. break
  113. default:
  114. break
  115. }
  116. }
  117. }