DataScannerViewController does not work on visionOS

81 Views Asked by At

i just started to test the DataScannerViewController on my Apple Vision Pro. The documentation says that it is available for vision OS 1.0+ (https://developer.apple.com/documentation/visionkit/datascannerviewcontroller), but if i start the Sample Code from here: https://developer.apple.com/documentation/visionkit/scanning-data-with-the-camera. The Log says it is not supported.

Does anyone know whats going on?

@MainActor
struct DocumentScannerView: UIViewControllerRepresentable {
    
    func updateUIViewController(_ uiViewController: DataScannerViewController, context: Context) {
        
    }
    
    
    var scannerViewController: DataScannerViewController = DataScannerViewController(
        recognizedDataTypes: [.text(), .barcode()],
        qualityLevel: .accurate,
        recognizesMultipleItems: false,
        isHighFrameRateTrackingEnabled: false,
        isHighlightingEnabled: true
    )
    
    func makeUIViewController(context: Context) -> DataScannerViewController {
        scannerViewController.delegate = context.coordinator
        
        // Add a button to start scanning
        let scanButton = UIButton(type: .system)
        scanButton.backgroundColor = UIColor.systemBlue
        scanButton.setTitle("Start Scan", for: .normal)
        scanButton.setTitleColor(UIColor.white, for: .normal)
        scanButton.addTarget(context.coordinator, action: #selector(Coordinator.startScanning(_:)), for: .touchUpInside)
        scannerViewController.view.addSubview(scanButton)
        
        // Set up button constraints
        scanButton.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            scanButton.centerXAnchor.constraint(equalTo: scannerViewController.view.centerXAnchor),
            scanButton.bottomAnchor.constraint(equalTo: scannerViewController.view.safeAreaLayoutGuide.bottomAnchor, constant: -20)
        ])
        
        return scannerViewController
    }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }

    
    class Coordinator: NSObject, DataScannerViewControllerDelegate {
        var parent: DocumentScannerView
        var roundBoxMappings: [UUID: UIView] = [:]
        
        init(_ parent: DocumentScannerView) {
            self.parent = parent
        }
        
        // DataScannerViewControllerDelegate - methods starts here
        func dataScanner(_ dataScanner: DataScannerViewController, didAdd addedItems: [RecognizedItem], allItems: [RecognizedItem]) {
            //ToDo
        }
        
        func dataScanner(_ dataScanner: DataScannerViewController, didRemove removedItems: [RecognizedItem], allItems: [RecognizedItem]) {
            //ToDo
        }
        
        func dataScanner(_ dataScanner: DataScannerViewController, didUpdate updatedItems: [RecognizedItem], allItems: [RecognizedItem]) {
            //ToDo
        }
        
        func dataScanner(_ dataScanner: DataScannerViewController, didTapOn item: RecognizedItem) {
            //ToDo
        }
        // DataScannerViewControllerDelegate - methods ends here
        
        // Add this method to start scanning
        @objc func startScanning(_ sender: UIButton) {
            do {
                try parent.scannerViewController.startScanning()
            } catch let error {
                print("Error: \(error.localizedDescription)")
            }
            
        }
    }
}
1

There are 1 best solutions below

2
lorem ipsum On

Apple has provided a compatibility list here

The following features aren’t available in compatible iPad and iPhone apps in visionOS. Use framework APIs to determine when the features are available.

Video or still-photo capture

Camera features like auto-focus or flash

Rear-facing (selfie) cameras

Vision. Data scanners do nothing in VisionKit.

For this particular ViewController you can use to confirm compatibility.

if DataScannerViewController.isSupported && DataScannerViewController.isAvailable{
    // Available
} else {
    // Show error, visionOS will enter here because while it is available it is not supported.
}

The "Notes" app uses the iPhone to scan the document and then passes it on to the VisionPro via iCloud.