Blame view

App/camera/ALCameraLib/Utilities/PhotoLibraryAuthorizer.swift 1.29 KB
1341bf603   Trịnh Văn Quân   version 1.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  //
  //  PhotoLibraryAuthorizer.swift
  //  ALCameraViewController
  //
  //  Created by Alex Littlejohn on 2016/03/26.
  //  Copyright © 2016 zero. All rights reserved.
  //
  
  import UIKit
  import Photos
  
  public typealias PhotoLibraryAuthorizerCompletion = (NSError?) -> Void
  
  class PhotoLibraryAuthorizer {
  
      private let errorDomain = "com.zero.imageFetcher"
  
      private let completion: PhotoLibraryAuthorizerCompletion
  
      init(completion: @escaping PhotoLibraryAuthorizerCompletion) {
          self.completion = completion
          handleAuthorization(status: PHPhotoLibrary.authorizationStatus())
      }
      
      func onDeniedOrRestricted(completion: PhotoLibraryAuthorizerCompletion) {
          let error = errorWithKey("error.access-denied", domain: errorDomain)
          completion(error)
      }
      
      func handleAuthorization(status: PHAuthorizationStatus) {
          switch status {
          case .notDetermined:
              PHPhotoLibrary.requestAuthorization(handleAuthorization)
              break
          case .authorized:
              DispatchQueue.main.async {
                  self.completion(nil)
              }
              break
          case .denied, .restricted:
              DispatchQueue.main.async {
                  self.onDeniedOrRestricted(completion: self.completion)
              }
              break
          }
      }
  }