Skip to content

Commit f5ee9a5

Browse files
authored
Merge pull request #73 from uniqby/main
fixes #72 Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
2 parents 04ea970 + 0e3683b commit f5ee9a5

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

Sources/CodeScanner/ScannerViewController.swift

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ extension CodeScannerView {
126126

127127
#else
128128

129-
var captureSession: AVCaptureSession!
129+
var captureSession: AVCaptureSession?
130130
var previewLayer: AVCaptureVideoPreviewLayer!
131131
let fallbackVideoCaptureDevice = AVCaptureDevice.default(for: .video)
132132

@@ -162,7 +162,7 @@ extension CodeScannerView {
162162

163163
@objc func updateOrientation() {
164164
guard let orientation = view.window?.windowScene?.interfaceOrientation else { return }
165-
guard let connection = captureSession.connections.last, connection.isVideoOrientationSupported else { return }
165+
guard let connection = captureSession?.connections.last, connection.isVideoOrientationSupported else { return }
166166
connection.videoOrientation = AVCaptureVideoOrientation(rawValue: orientation.rawValue) ?? .portrait
167167
}
168168

@@ -174,6 +174,14 @@ extension CodeScannerView {
174174
override public func viewWillAppear(_ animated: Bool) {
175175
super.viewWillAppear(animated)
176176

177+
setupSession()
178+
}
179+
180+
private func setupSession() {
181+
guard let captureSession = captureSession else {
182+
return
183+
}
184+
177185
if previewLayer == nil {
178186
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
179187
}
@@ -185,37 +193,41 @@ extension CodeScannerView {
185193

186194
delegate?.reset()
187195

188-
if (captureSession?.isRunning == false) {
196+
if (captureSession.isRunning == false) {
189197
DispatchQueue.global(qos: .userInteractive).async {
190-
self.captureSession.startRunning()
198+
self.captureSession?.startRunning()
191199
}
192200
}
193201
}
194-
202+
195203
private func handleCameraPermission() {
196-
switch AVCaptureDevice.authorizationStatus(for: .video) {
197-
case .denied, .restricted:
198-
break
199-
case .notDetermined:
200-
self.requestCameraAccess {
201-
self.setupCaptureDevice()
204+
switch AVCaptureDevice.authorizationStatus(for: .video) {
205+
case .denied, .restricted:
206+
break
207+
case .notDetermined:
208+
self.requestCameraAccess {
209+
self.setupCaptureDevice()
210+
DispatchQueue.main.async {
211+
self.setupSession()
212+
}
213+
}
214+
case .authorized:
215+
self.setupCaptureDevice()
216+
self.setupSession()
217+
218+
default:
219+
break
202220
}
203-
case .authorized:
204-
self.setupCaptureDevice()
205-
return
206-
default:
207-
break
208-
}
209221
}
210222

211-
private func requestCameraAccess(completion: (() -> Void)?) {
212-
AVCaptureDevice.requestAccess(for: .video) { [weak self] status in
213-
guard status else {
214-
self?.delegate?.didFail(reason: .permissionDenied)
215-
return
223+
private func requestCameraAccess(completion: (() -> Void)?) {
224+
AVCaptureDevice.requestAccess(for: .video) { [weak self] status in
225+
guard status else {
226+
self?.delegate?.didFail(reason: .permissionDenied)
227+
return
228+
}
229+
completion?()
216230
}
217-
completion?()
218-
}
219231
}
220232

221233
private func addOrientationDidChangeObserver() {
@@ -247,17 +259,17 @@ extension CodeScannerView {
247259
return
248260
}
249261

250-
if (captureSession.canAddInput(videoInput)) {
251-
captureSession.addInput(videoInput)
262+
if (captureSession!.canAddInput(videoInput)) {
263+
captureSession!.addInput(videoInput)
252264
} else {
253265
delegate?.didFail(reason: .badInput)
254266
return
255267
}
256268

257269
let metadataOutput = AVCaptureMetadataOutput()
258270

259-
if (captureSession.canAddOutput(metadataOutput)) {
260-
captureSession.addOutput(metadataOutput)
271+
if (captureSession!.canAddOutput(metadataOutput)) {
272+
captureSession!.addOutput(metadataOutput)
261273

262274
metadataOutput.setMetadataObjectsDelegate(delegate, queue: DispatchQueue.main)
263275
metadataOutput.metadataObjectTypes = delegate?.parent.codeTypes
@@ -285,7 +297,7 @@ extension CodeScannerView {
285297

286298
if (captureSession?.isRunning == true) {
287299
DispatchQueue.global(qos: .userInteractive).async {
288-
self.captureSession.stopRunning()
300+
self.captureSession?.stopRunning()
289301
}
290302
}
291303

0 commit comments

Comments
 (0)