Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : MacLustr Storage Node9// File : MacLustrStorageNode/Features/Onboarding/QRScannerView.swift10// Description : Camera view that reads the pairing QR code. The camera is11// used for nothing else and no image is kept.12// Module : MacLustrStorageNode13// Repository : https://git.spboucher.ai/maclustr-storage-ios14// Project Website : https://storage.spboucher.ai15// Created : 2026-12-0116// Last Modified : 2026-12-0117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author : Simon-Pierre Boucher23// Position : Professor of Finance24// Institution : Université du Québec en Outaouais (UQO)25// Department : Département des sciences administratives26// Organization : SP Boucher27//28// Website : https://spboucher.ai29// Git : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General : info@spboucher.ai36// Professional : contact@spboucher.ai37// Direct : simon@spboucher.ai38// Research : research@spboucher.ai39// Development : dev@spboucher.ai40// Support : support@spboucher.ai41// Security/Admin : admin@spboucher.ai42// Notifications : notifications@spboucher.ai43//44// Preferred Contact: admin@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification : Public69// Distribution : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License : None specified (no license file in this repository)83// Maintainer : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import SwiftUI91import AVFoundation9293struct QRScannerView: UIViewControllerRepresentable {94 let onCode: (String) -> Void95 func makeUIViewController(context: Context) -> ScannerController { let c = ScannerController(); c.onCode = onCode; return c }96 func updateUIViewController(_ controller: ScannerController, context: Context) {}9798 final class ScannerController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {99 var onCode: ((String) -> Void)?100 private let session = AVCaptureSession()101 private var preview: AVCaptureVideoPreviewLayer?102 private var delivered = false103 private let message = UILabel()104105 override func viewDidLoad() {106 super.viewDidLoad()107 view.backgroundColor = .black108 message.textColor = .lightGray; message.numberOfLines = 0; message.textAlignment = .center; message.font = .preferredFont(forTextStyle: .footnote)109 message.translatesAutoresizingMaskIntoConstraints = false110 view.addSubview(message)111 NSLayoutConstraint.activate([message.centerYAnchor.constraint(equalTo: view.centerYAnchor), message.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 24), message.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -24)])112 AVCaptureDevice.requestAccess(for: .video) { granted in DispatchQueue.main.async { granted ? self.configure() : self.show("Camera access is off. Allow it in iOS Settings, or type the pairing code instead.") } }113 }114 private func show(_ text: String) { message.text = text }115 private func configure() {116 guard let device = AVCaptureDevice.default(for: .video), let input = try? AVCaptureDeviceInput(device: device), session.canAddInput(input) else { show("No camera available on this device. Type the pairing code instead."); return }117 session.addInput(input)118 let output = AVCaptureMetadataOutput()119 guard session.canAddOutput(output) else { return }120 session.addOutput(output)121 output.setMetadataObjectsDelegate(self, queue: .main)122 output.metadataObjectTypes = [.qr]123 let layer = AVCaptureVideoPreviewLayer(session: session)124 layer.videoGravity = .resizeAspectFill; layer.frame = view.bounds125 view.layer.insertSublayer(layer, at: 0); preview = layer126 DispatchQueue.global(qos: .userInitiated).async { self.session.startRunning() }127 }128 override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews(); preview?.frame = view.bounds }129 override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated); if session.isRunning { session.stopRunning() } }130 func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput objects: [AVMetadataObject], from connection: AVCaptureConnection) {131 guard !delivered, let value = (objects.first as? AVMetadataMachineReadableCodeObject)?.stringValue, value.lowercased().hasPrefix("msp1://") else { return }132 delivered = true133 UINotificationFeedbackGenerator().notificationOccurred(.success)134 onCode?(value)135 }136 }137}138