Skip to content

Commit c27af91

Browse files
authored
fix(barcode-scanner): check if iOS platform supports scanning (#3222)
1 parent b60dd88 commit c27af91

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
barcode-scanner: patch
3+
barcode-scanner-js: patch
4+
---
5+
6+
On iOS, fixed an application crash happening when the scanner was started on the iOS Simulator (no camera available).

plugins/barcode-scanner/ios/Sources/BarcodeScannerPlugin.swift

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,20 +269,20 @@ class BarcodeScannerPlugin: Plugin, AVCaptureMetadataOutputObjectsDelegate {
269269
scanFormats = [AVMetadataObject.ObjectType]()
270270

271271
(args.formats ?? []).forEach { format in
272-
if let formatValue = format.value {
273-
scanFormats.append(formatValue)
274-
} else {
275-
invoke.reject("Unsupported barcode format on this iOS version: \(format)")
276-
return
277-
}
272+
if let formatValue = format.value {
273+
scanFormats.append(formatValue)
274+
} else {
275+
invoke.reject("Unsupported barcode format on this iOS version: \(format)")
276+
return
277+
}
278278
}
279279

280280
if scanFormats.isEmpty {
281-
for supportedFormat in SupportedFormat.allCases {
282-
if let formatValue = supportedFormat.value {
283-
scanFormats.append(formatValue)
284-
}
281+
for supportedFormat in SupportedFormat.allCases {
282+
if let formatValue = supportedFormat.value {
283+
scanFormats.append(formatValue)
285284
}
285+
}
286286
}
287287

288288
self.metaOutput!.metadataObjectTypes = self.scanFormats
@@ -305,6 +305,13 @@ class BarcodeScannerPlugin: Plugin, AVCaptureMetadataOutputObjectsDelegate {
305305
return
306306
}
307307

308+
// Check if camera is available on this platform (iOS simulator doesn't have cameras)
309+
let availableVideoDevices = discoverCaptureDevices()
310+
if availableVideoDevices.isEmpty {
311+
invoke.reject("No camera available on this device (e.g., iOS Simulator)")
312+
return
313+
}
314+
308315
var iOS14min: Bool = false
309316
if #available(iOS 14.0, *) { iOS14min = true }
310317
if !iOS14min && self.getPermissionState() != "granted" {

0 commit comments

Comments
 (0)