ZonePermissions.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // ZonePermissions.swift
  3. // ZoneBarManager
  4. //
  5. // Created by 刘振兴 on 2017/3/14.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. import AVFoundation
  10. import Photos
  11. import AssetsLibrary
  12. public enum ZoneAuthorizationStatus:NSInteger {
  13. case zAuthorizationStatusAuthorized = 0, //正常
  14. zAuthorizationStatusDenied, //没有权限
  15. zAuthorizationStatusRestricted, //受限制
  16. zAuthorizationStatusNotSupport //不支持
  17. }
  18. public class ZonePermissions: NSObject {
  19. public class func requestImagePickerAuthorization(callback:@escaping (_ authorizationStatus:ZoneAuthorizationStatus) -> Void){
  20. if(UIImagePickerController.isSourceTypeAvailable(.camera) || UIImagePickerController.isSourceTypeAvailable(.photoLibrary)){
  21. let authStatus = PHPhotoLibrary.authorizationStatus()
  22. switch authStatus {
  23. case .notDetermined:
  24. PHPhotoLibrary.requestAuthorization({ (status) in
  25. if(status == PHAuthorizationStatus.authorized){
  26. DispatchQueue.main.async {
  27. callback(ZoneAuthorizationStatus.zAuthorizationStatusAuthorized)
  28. }
  29. }else if(status == PHAuthorizationStatus.denied){
  30. DispatchQueue.main.async {
  31. callback(ZoneAuthorizationStatus.zAuthorizationStatusDenied)
  32. }
  33. }else if(authStatus == PHAuthorizationStatus.restricted){
  34. DispatchQueue.main.async {
  35. callback(ZoneAuthorizationStatus.zAuthorizationStatusRestricted)
  36. }
  37. }
  38. })
  39. case .authorized:
  40. DispatchQueue.main.async {
  41. callback(ZoneAuthorizationStatus.zAuthorizationStatusAuthorized)
  42. }
  43. case .denied:
  44. DispatchQueue.main.async {
  45. callback(ZoneAuthorizationStatus.zAuthorizationStatusDenied)
  46. }
  47. case .restricted:
  48. DispatchQueue.main.async {
  49. callback(ZoneAuthorizationStatus.zAuthorizationStatusRestricted)
  50. }
  51. case .limited:
  52. DispatchQueue.main.async {
  53. callback(ZoneAuthorizationStatus.zAuthorizationStatusAuthorized)
  54. }
  55. }
  56. }else {
  57. DispatchQueue.main.async {
  58. callback(ZoneAuthorizationStatus.zAuthorizationStatusNotSupport)
  59. }
  60. }
  61. }
  62. public class func requestCameraAuthorization(callback:@escaping (_ authorizationStatus:ZoneAuthorizationStatus) -> Void){
  63. if(UIImagePickerController.isSourceTypeAvailable(.camera)){
  64. let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
  65. switch authStatus {
  66. case .notDetermined:
  67. AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (granted) in
  68. if granted == true {
  69. DispatchQueue.main.async {
  70. callback(ZoneAuthorizationStatus.zAuthorizationStatusAuthorized)
  71. }
  72. }else{
  73. DispatchQueue.main.async {
  74. callback(ZoneAuthorizationStatus.zAuthorizationStatusDenied)
  75. }
  76. }
  77. })
  78. case .authorized:
  79. DispatchQueue.main.async {
  80. callback(ZoneAuthorizationStatus.zAuthorizationStatusAuthorized)
  81. }
  82. case .denied:
  83. DispatchQueue.main.async {
  84. callback(ZoneAuthorizationStatus.zAuthorizationStatusDenied)
  85. }
  86. case .restricted:
  87. DispatchQueue.main.async {
  88. callback(ZoneAuthorizationStatus.zAuthorizationStatusRestricted)
  89. }
  90. }
  91. }else{
  92. DispatchQueue.main.async {
  93. callback(ZoneAuthorizationStatus.zAuthorizationStatusNotSupport)
  94. }
  95. }
  96. }
  97. public class func requestAddressBookAuthorization(callback:(_ authorizationStatus:ZoneAuthorizationStatus) -> Void){
  98. }
  99. }