Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 71b67e8

Browse files
committed
Start camera on appear instead of load
1 parent 45d461f commit 71b67e8

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

Sources/ScanKit/NSCodeScanner.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ public class NSCodeScanner: NSViewController, AVCaptureVideoDataOutputSampleBuff
6868

6969
public override func viewDidLoad() {
7070
super.viewDidLoad()
71-
7271
addPreviewLayer()
72+
}
73+
74+
override public func viewDidAppear() {
7375

7476
// Start the camera
7577
Task.detached(priority: .userInitiated) { [weak self] in

Sources/ScanKit/ScanKitCamera.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public class ScanKitCamera: NSObject, ObservableObject, AVCaptureVideoDataOutput
9797
/// Creates a SwiftUI `View` containing all available cameras as `Button`s.
9898
/// When a button is selected, the camera it represents will be made active.
9999
/// - Returns: A `View` containing a dynamic number of `Button`s.
100-
func getCamerasAsButtons() -> some View {
100+
public func getCamerasAsButtons() -> some View {
101101
ForEach(availableCaptureDevices, id: \.modelID) { device in
102102
Button(device.localizedName, action: {
103103
self.captureDevice = device
@@ -106,30 +106,30 @@ public class ScanKitCamera: NSObject, ObservableObject, AVCaptureVideoDataOutput
106106
}
107107

108108
/// A Boolean value indicating whether or not the camera is active.
109-
var isCapturing: Bool {
109+
public var isCapturing: Bool {
110110
captureSession.isRunning
111111
}
112112

113113
/// A Boolean value indicating whether or not the camera is front-facing. On macOS this is always false.
114-
var isUsingFrontCamera: Bool {
114+
public var isUsingFrontCamera: Bool {
115115
guard let captureDevice = captureDevice else { return false }
116116
return frontCaptureDevices.contains(captureDevice)
117117
}
118118

119119
/// A Boolean value indicating whether or not the camera is rear-facing. On macOS this is always false.
120-
var isUsingBackCamera: Bool {
120+
public var isUsingBackCamera: Bool {
121121
guard let captureDevice = captureDevice else { return false }
122122
return backCaptureDevices.contains(captureDevice)
123123
}
124124

125125
/// A Boolean value indicating whether or not the current device has multiple cameras available to access.
126-
var hasMultipleCaptureDevices: Bool {
126+
public var hasMultipleCaptureDevices: Bool {
127127
availableCaptureDevices.count > 1
128128
}
129129

130130
/// Switch between known capture devices cyclically.
131131
/// On iOS devices this function cycles between only the front and rear cameras.
132-
func cycleCaptureDevices() {
132+
public func cycleCaptureDevices() {
133133
if let captureDevice = captureDevice, let index = availableCaptureDevices.firstIndex(of: captureDevice) {
134134
let nextIndex = (index + 1) % availableCaptureDevices.count
135135
self.captureDevice = availableCaptureDevices[nextIndex]
@@ -142,7 +142,7 @@ public class ScanKitCamera: NSObject, ObservableObject, AVCaptureVideoDataOutput
142142
// MARK: - Init/Configure
143143

144144
/// Create a ScanKit camera with the first available camera.
145-
override init() {
145+
override public init() {
146146
super.init()
147147
initialize()
148148
}
@@ -280,18 +280,18 @@ public class ScanKitCamera: NSObject, ObservableObject, AVCaptureVideoDataOutput
280280
// MARK: - Torch
281281

282282
/// A Boolean value indicating whether a persistent flash, or *torch*, is available.
283-
var isTorchAvailable: Bool {
283+
public var isTorchAvailable: Bool {
284284
return captureDevice?.isTorchModeSupported(.on) ?? false
285285
}
286286

287287
/// A Boolean value indicating whether or the persistent flash, or *torch*, is active.
288-
var isTorchOn: Bool {
288+
public var isTorchOn: Bool {
289289
captureDevice?.isTorchActive ?? false
290290
}
291291

292292
/// Toggles the camera's persistent flash, or *torch*, if available.
293293
/// If ``isTorchAvailable`` is false this function does nothing.
294-
func toggleTorch() {
294+
public func toggleTorch() {
295295
if isTorchAvailable, let captureDevice = captureDevice {
296296
do {
297297
try captureDevice.lockForConfiguration()
@@ -311,7 +311,7 @@ public class ScanKitCamera: NSObject, ObservableObject, AVCaptureVideoDataOutput
311311
/// Start capturing video from the camera.
312312
/// - Warning: ``stop()`` must be called to deactivate the camera.
313313
/// Leaving the camera active after use is a resource waste and a poor user experience.
314-
func start() async {
314+
public func start() async {
315315
guard await checkAuthorization() else {
316316
if self.isScanning { addToResultStream?(.failure(ScanKitError.notAuthorized)) }
317317
return
@@ -336,7 +336,7 @@ public class ScanKitCamera: NSObject, ObservableObject, AVCaptureVideoDataOutput
336336
}
337337

338338
/// Stop capturing video from the camera. If``isCapturing`` is false, this function does nothing.
339-
func stop() {
339+
public func stop() {
340340
guard isCaptureSessionConfigured, isCapturing else { return }
341341

342342
sessionQueue.async { [weak self] in
@@ -361,14 +361,14 @@ public class ScanKitCamera: NSObject, ObservableObject, AVCaptureVideoDataOutput
361361
private let sequenceHandler = VNSequenceRequestHandler()
362362

363363
/// A Boolean value indicating whether or not the camera is scanning for machine readable codes.
364-
var isScanning: Bool = false
364+
public var isScanning: Bool = false
365365

366366
/// The current symbology being detected by the camera.
367367
/// Use the ``supportedSymbologies`` property to indicate the specific symbologies the request detects.
368-
var symbology: VNBarcodeSymbology = .qr
368+
public var symbology: VNBarcodeSymbology = .qr
369369

370370
/// An array of symbologies supported by the scanner.
371-
var supportedSymbologies: [VNBarcodeSymbology] {
371+
public var supportedSymbologies: [VNBarcodeSymbology] {
372372
VNDetectBarcodesRequest.supportedSymbologies
373373
}
374374

@@ -421,7 +421,7 @@ public class ScanKitCamera: NSObject, ObservableObject, AVCaptureVideoDataOutput
421421
///
422422
/// - Warning: This stream subscribes absolutely to the scanning process and will be retained until `isScanning` is set to `false`.
423423
/// - Returns: An `AsyncThrowingStream` which yields `String` on success and `ScanKitError` on failure.
424-
var resultsStream: AsyncThrowingStream<String, Error> {
424+
public var resultsStream: AsyncThrowingStream<String, Error> {
425425
return AsyncThrowingStream<String, Error> { continuation in
426426
addToResultStream = { [weak self] result in
427427
if (self?.isScanning ?? false) {

Sources/ScanKit/UICodeScanner.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ public class UICodeScanner: UIViewController {
7373
public override func viewDidLoad() {
7474
super.viewDidLoad()
7575
addPreviewLayer()
76-
76+
}
77+
78+
public override func viewDidAppear(_ animated: Bool) {
7779
// Start the camera
7880
Task.detached(priority: .userInitiated) { [weak self] in
7981
await self?.camera?.start()

0 commit comments

Comments
 (0)