// THE CAMERA THAT READS A NEW DEVICE'S SQUARE CODE (GETTING-IN.md, 22 Nov 2026).//// After Approve and the phone's lock, the card opens this over the app: the// camera, `scan.heading` over it ("Scan the code on your Mac"), a wrong scan's// sentence under it, and Type the code instead. It reads QR codes only and// hands the text to the card untouched (`signin.approve {scanned}`); it never// opens a link, and the core decides whether the text is a Cotton code for this// request. VisionKit's DataScannerViewController; where it is not supported// (no camera, a simulator) `available` is false and the card goes straight to// the digits.importCottonKitimportSwiftUI#if os(iOS) && canImport(VisionKit)importVisionKit#endifstructSquareScanner: View {let heading: Stringlet typeInstead: Stringlet wrong: String?let onRead: (String) -> Voidlet onTypeInstead: () -> Voidlet onClose: () -> Void/// Whether this phone can scan at all: a camera, VisionKit's scanner, and the person allowing it. @MainActor staticvar available: Bool { #if os(iOS) && canImport(VisionKit) && !targetEnvironment(simulator)// An app with no camera purpose string would be stopped by iOS: it goes straight to the digits instead.guardBundle.main.object(forInfoDictionaryKey: "NSCameraUsageDescription") != nilelse { returnfalse }returnDataScannerViewController.isSupported && DataScannerViewController.isAvailable #elsereturnfalse #endif }var body: someView { ZStack {Color.black.ignoresSafeArea() #if os(iOS) && canImport(VisionKit) && !targetEnvironment(simulator)ScannerView(onRead: onRead).ignoresSafeArea() #endif VStack(spacing: 14) { HStack {Spacer()Button(action: onClose) {Image(systemName: "xmark").font(.system(size: 17, weight: .semibold)).foregroundStyle(.white) .padding(12).background(.black.opacity(0.45), in: Circle()) } .accessibilityLabel(Text(ShellLines.line("chrome", "sheet.close"))) .accessibilityIdentifier("scan-close") }Text(heading) .font(.title3.weight(.semibold)) .foregroundStyle(.white) .multilineTextAlignment(.center) .fixedSize(horizontal: false, vertical: true) .padding(.horizontal, 20) .accessibilityIdentifier("scan-heading")Spacer()iflet wrong {Text(wrong) .font(.callout) .foregroundStyle(.white) .multilineTextAlignment(.center) .fixedSize(horizontal: false, vertical: true) .padding(12) .background(.red.opacity(0.75), in: RoundedRectangle(cornerRadius: 12, style: .continuous)) .padding(.horizontal, 20) .accessibilityIdentifier("scan-wrong") }Button(typeInstead, action: onTypeInstead) .font(.body.weight(.semibold)) .foregroundStyle(.white) .padding(.vertical, 12) .padding(.horizontal, 20) .background(.black.opacity(0.45), in: Capsule()) .accessibilityIdentifier("scan-type-instead") } .padding(16) } }}#if os(iOS) && canImport(VisionKit) && !targetEnvironment(simulator)/// VisionKit's scanner for QR codes; each new code's text is read once.privatestructScannerView: UIViewControllerRepresentable {let onRead: (String) -> Voidfunc makeCoordinator() -> Reader { Reader(onRead: onRead) }func makeUIViewController(context: Context) -> DataScannerViewController {let scanner = DataScannerViewController( recognizedDataTypes: [.barcode(symbologies: [.qr])], qualityLevel: .balanced, recognizesMultipleItems: false, isHighFrameRateTrackingEnabled: false, isPinchToZoomEnabled: true, isGuidanceEnabled: true, isHighlightingEnabled: true ) scanner.delegate = context.coordinatortry? scanner.startScanning()return scanner }func updateUIViewController(_ scanner: DataScannerViewController, context: Context) { context.coordinator.onRead = onReadif !scanner.isScanning { try? scanner.startScanning() } }staticfunc dismantleUIViewController(_ scanner: DataScannerViewController, coordinator: Reader) { scanner.stopScanning() } @MainActorfinalclassReader: NSObject, DataScannerViewControllerDelegate {var onRead: (String) -> Void/// The last text handed on: the same code held in view is read once, a different one again.privatevar last: String?init(onRead: @escaping (String) -> Void) { self.onRead = onRead }func dataScanner(_ dataScanner: DataScannerViewController, didAdd addedItems: [RecognizedItem], allItems: [RecognizedItem]) {for item in addedItems {guardcase .barcode(let code) = item, let text = code.payloadStringValue, text != last else { continue } last = text onRead(text)return } } }}#endif