Julian Steenbakker
Committed by GitHub

Merge pull request #896 from navaronbracke/fix_ios_17_camera_crash

fix: Use default selectors instead of a discovery session
@@ -69,6 +69,43 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -69,6 +69,43 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
69 super.init() 69 super.init()
70 } 70 }
71 71
  72 + /// Get the default camera device for the given `position`.
  73 + ///
  74 + /// This function selects the most appropriate camera, when it is available.
  75 + private func getDefaultCameraDevice(position: AVCaptureDevice.Position) -> AVCaptureDevice? {
  76 + if #available(iOS 13.0, *) {
  77 + // Find the built-in Triple Camera, if it exists.
  78 + if let device = AVCaptureDevice.default(.builtInTripleCamera,
  79 + for: .video,
  80 + position: position) {
  81 + return device
  82 + }
  83 +
  84 + // Find the built-in Dual-Wide Camera, if it exists.
  85 + if let device = AVCaptureDevice.default(.builtInDualWideCamera,
  86 + for: .video,
  87 + position: position) {
  88 + return device
  89 + }
  90 + }
  91 +
  92 + // Find the built-in Dual Camera, if it exists.
  93 + if let device = AVCaptureDevice.default(.builtInDualCamera,
  94 + for: .video,
  95 + position: position) {
  96 + return device
  97 + }
  98 +
  99 + // Find the built-in Wide-Angle Camera, if it exists.
  100 + if let device = AVCaptureDevice.default(.builtInWideAngleCamera,
  101 + for: .video,
  102 + position: position) {
  103 + return device
  104 + }
  105 +
  106 + return nil
  107 + }
  108 +
72 /// Check if we already have camera permission. 109 /// Check if we already have camera permission.
73 func checkPermission() -> Int { 110 func checkPermission() -> Int {
74 let status = AVCaptureDevice.authorizationStatus(for: .video) 111 let status = AVCaptureDevice.authorizationStatus(for: .video)
@@ -146,11 +183,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -146,11 +183,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
146 textureId = registry?.register(self) 183 textureId = registry?.register(self)
147 184
148 // Open the camera device 185 // Open the camera device
149 - if #available(iOS 13.0, *) {  
150 - device = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTripleCamera, .builtInDualWideCamera, .builtInDualCamera, .builtInWideAngleCamera], mediaType: .video, position: cameraPosition).devices.first  
151 - } else {  
152 - device = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera, .builtInWideAngleCamera], mediaType: .video, position: cameraPosition).devices.first  
153 - } 186 + device = getDefaultCameraDevice(position: cameraPosition)
154 187
155 if (device == nil) { 188 if (device == nil) {
156 throw MobileScannerError.noCamera 189 throw MobileScannerError.noCamera