|
| 1 | +// |
| 2 | +// SLRPermissionDelegateImpl.swift |
| 3 | +// StreamLayerPlugins |
| 4 | +// |
| 5 | +// Copyright © 2024 StreamLayer, Inc. All rights reserved. |
| 6 | +// |
| 7 | + |
| 8 | +#if os(iOS) |
| 9 | +import UIKit |
| 10 | +import AVFoundation |
| 11 | +import Contacts |
| 12 | +import Photos |
| 13 | +import StreamLayerSDK |
| 14 | + |
| 15 | +final class SLRPermissionDelegateImpl: NSObject, SLRPermissionDelegate { |
| 16 | + |
| 17 | + // MARK: - Permission Status |
| 18 | + |
| 19 | + func permissionStatus(for type: SLRPermissionType) -> SLRPermissionStatus { |
| 20 | + switch type { |
| 21 | + case .camera: |
| 22 | + switch AVCaptureDevice.authorizationStatus(for: .video) { |
| 23 | + case .authorized: return .authorized |
| 24 | + case .denied: return .denied |
| 25 | + case .notDetermined: return .notDetermined |
| 26 | + case .restricted: return .restricted |
| 27 | + @unknown default: return .denied |
| 28 | + } |
| 29 | + case .microphone: |
| 30 | + switch AVAudioSession.sharedInstance().recordPermission { |
| 31 | + case .granted: return .authorized |
| 32 | + case .denied: return .denied |
| 33 | + case .undetermined: return .notDetermined |
| 34 | + @unknown default: return .denied |
| 35 | + } |
| 36 | + case .contacts: |
| 37 | + switch CNContactStore.authorizationStatus(for: .contacts) { |
| 38 | + case .authorized: return .authorized |
| 39 | + case .denied: return .denied |
| 40 | + case .notDetermined: return .notDetermined |
| 41 | + case .restricted: return .restricted |
| 42 | + @unknown default: return .denied |
| 43 | + } |
| 44 | + case .photoLibrary: |
| 45 | + switch PHPhotoLibrary.authorizationStatus() { |
| 46 | + case .authorized, .limited: return .authorized |
| 47 | + case .denied: return .denied |
| 48 | + case .notDetermined: return .notDetermined |
| 49 | + case .restricted: return .restricted |
| 50 | + @unknown default: return .denied |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // MARK: - Permission Requests |
| 56 | + |
| 57 | + func requestPermission(for type: SLRPermissionType, completion: @escaping (Bool) -> Void) { |
| 58 | + switch type { |
| 59 | + case .camera: |
| 60 | + AVCaptureDevice.requestAccess(for: .video) { granted in |
| 61 | + DispatchQueue.main.async { completion(granted) } |
| 62 | + } |
| 63 | + case .microphone: |
| 64 | + AVAudioSession.sharedInstance().requestRecordPermission { granted in |
| 65 | + DispatchQueue.main.async { completion(granted) } |
| 66 | + } |
| 67 | + case .contacts: |
| 68 | + CNContactStore().requestAccess(for: .contacts) { granted, _ in |
| 69 | + DispatchQueue.main.async { completion(granted) } |
| 70 | + } |
| 71 | + case .photoLibrary: |
| 72 | + PHPhotoLibrary.requestAuthorization { status in |
| 73 | + DispatchQueue.main.async { completion(status == .authorized || status == .limited) } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // MARK: - Contacts Data Access |
| 79 | + |
| 80 | + func fetchContacts(completion: @escaping ([SLRContactInfo]) -> Void) { |
| 81 | + DispatchQueue.global(qos: .userInitiated).async { |
| 82 | + let store = CNContactStore() |
| 83 | + let keysToFetch: [CNKeyDescriptor] = [ |
| 84 | + CNContactIdentifierKey as CNKeyDescriptor, |
| 85 | + CNContactGivenNameKey as CNKeyDescriptor, |
| 86 | + CNContactFamilyNameKey as CNKeyDescriptor, |
| 87 | + CNContactPhoneNumbersKey as CNKeyDescriptor, |
| 88 | + CNContactThumbnailImageDataKey as CNKeyDescriptor |
| 89 | + ] |
| 90 | + let request = CNContactFetchRequest(keysToFetch: keysToFetch) |
| 91 | + var contacts: [SLRContactInfo] = [] |
| 92 | + |
| 93 | + do { |
| 94 | + try store.enumerateContacts(with: request) { cnContact, _ in |
| 95 | + let phoneNumbers = cnContact.phoneNumbers.map { $0.value.stringValue } |
| 96 | + contacts.append(SLRContactInfo( |
| 97 | + identifier: cnContact.identifier, |
| 98 | + givenName: cnContact.givenName, |
| 99 | + familyName: cnContact.familyName, |
| 100 | + phoneNumbers: phoneNumbers, |
| 101 | + thumbnailImageData: cnContact.thumbnailImageData |
| 102 | + )) |
| 103 | + } |
| 104 | + } catch { |
| 105 | + print("Failed to fetch contacts: \(error)") |
| 106 | + } |
| 107 | + |
| 108 | + DispatchQueue.main.async { completion(contacts) } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + func fetchContact(identifier: String, completion: @escaping (SLRContactInfo?) -> Void) { |
| 113 | + DispatchQueue.global(qos: .userInitiated).async { |
| 114 | + let store = CNContactStore() |
| 115 | + let keysToFetch: [CNKeyDescriptor] = [ |
| 116 | + CNContactIdentifierKey as CNKeyDescriptor, |
| 117 | + CNContactGivenNameKey as CNKeyDescriptor, |
| 118 | + CNContactFamilyNameKey as CNKeyDescriptor, |
| 119 | + CNContactPhoneNumbersKey as CNKeyDescriptor, |
| 120 | + CNContactThumbnailImageDataKey as CNKeyDescriptor |
| 121 | + ] |
| 122 | + do { |
| 123 | + let cnContact = try store.unifiedContact(withIdentifier: identifier, |
| 124 | + keysToFetch: keysToFetch) |
| 125 | + let info = SLRContactInfo( |
| 126 | + identifier: cnContact.identifier, |
| 127 | + givenName: cnContact.givenName, |
| 128 | + familyName: cnContact.familyName, |
| 129 | + phoneNumbers: cnContact.phoneNumbers.map { $0.value.stringValue }, |
| 130 | + thumbnailImageData: cnContact.thumbnailImageData |
| 131 | + ) |
| 132 | + DispatchQueue.main.async { completion(info) } |
| 133 | + } catch { |
| 134 | + DispatchQueue.main.async { completion(nil) } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // MARK: - Photo Library Data Source |
| 140 | + |
| 141 | + func createPhotoLibraryDataSource() -> SLRPhotoLibraryDataSource? { |
| 142 | + return SLRPhotoLibraryDataSourceImpl() |
| 143 | + } |
| 144 | + |
| 145 | + // MARK: - Camera Capture Session |
| 146 | + |
| 147 | + func createCameraCaptureSession() -> SLRCameraCaptureSession? { |
| 148 | + return SLRCameraCaptureSessionImpl() |
| 149 | + } |
| 150 | +} |
| 151 | +#endif |
0 commit comments