LBXPermissions.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // LBXPermissions.swift
  3. // swiftScan
  4. //
  5. // Created by xialibing on 15/12/15.
  6. // Copyright © 2015年 xialibing. All rights reserved.
  7. //
  8. import UIKit
  9. import AVFoundation
  10. import Photos
  11. import AssetsLibrary
  12. class LBXPermissions: NSObject {
  13. //MARK: ----获取相册权限
  14. static func authorizePhotoWith(comletion: @escaping (Bool) -> Void) {
  15. let granted = PHPhotoLibrary.authorizationStatus()
  16. switch granted {
  17. case PHAuthorizationStatus.authorized:
  18. comletion(true)
  19. case PHAuthorizationStatus.denied, PHAuthorizationStatus.restricted:
  20. comletion(false)
  21. case PHAuthorizationStatus.notDetermined:
  22. PHPhotoLibrary.requestAuthorization({ status in
  23. DispatchQueue.main.async {
  24. comletion(status == PHAuthorizationStatus.authorized)
  25. }
  26. })
  27. @unknown default:
  28. comletion(false)
  29. }
  30. }
  31. //MARK: ---相机权限
  32. static func authorizeCameraWith(completion: @escaping (Bool) -> Void) {
  33. let granted = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
  34. switch granted {
  35. case .authorized:
  36. completion(true)
  37. case .denied:
  38. completion(false)
  39. case .restricted:
  40. completion(false)
  41. case .notDetermined:
  42. AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (granted: Bool) in
  43. DispatchQueue.main.async {
  44. completion(granted)
  45. }
  46. })
  47. @unknown default:
  48. completion(false)
  49. }
  50. }
  51. //MARK: 跳转到APP系统设置权限界面
  52. static func jumpToSystemPrivacySetting() {
  53. guard let appSetting = URL(string: UIApplication.openSettingsURLString) else {
  54. return
  55. }
  56. if #available(iOS 10, *) {
  57. UIApplication.shared.open(appSetting, options: [:], completionHandler: nil)
  58. } else {
  59. UIApplication.shared.openURL(appSetting)
  60. }
  61. }
  62. }