VideoPermissionService 2.swift 949 B

1234567891011121314151617181920212223242526272829303132333435
  1. import AVFoundation
  2. /// Service used to check authorization status of the capture device.
  3. final class VideoPermissionService {
  4. enum Error: Swift.Error {
  5. case notAuthorizedToUseCamera
  6. }
  7. // MARK: - Authorization
  8. /// Checks authorization status of the capture device.
  9. func checkPersmission(completion: @escaping (Error?) -> Void) {
  10. switch AVCaptureDevice.authorizationStatus(for: .video) {
  11. case .authorized:
  12. completion(nil)
  13. case .notDetermined:
  14. askForPermissions(completion)
  15. default:
  16. completion(Error.notAuthorizedToUseCamera)
  17. }
  18. }
  19. /// Asks for permission to use video.
  20. private func askForPermissions(_ completion: @escaping (Error?) -> Void) {
  21. AVCaptureDevice.requestAccess(for: .video) { granted in
  22. DispatchQueue.main.async {
  23. guard granted else {
  24. completion(Error.notAuthorizedToUseCamera)
  25. return
  26. }
  27. completion(nil)
  28. }
  29. }
  30. }
  31. }