How to scan QR codes with VisionKit for iOS
Learn how to use the DataScannerViewController to scan QR codes.
10 Apr 2023 · 3 min read
Starting with iOS 16, Apple introduced a data scanner in VisionKit, which allows us to to scan data in the live video stream from the iOS camera. Besides different text types, it supports recognition for various barcode types including automatic item highlighting.
Let's look at how we can implement the data scanner functionality for QR codes.

Overview
To implement a data scanner for QR codes, the following steps are required:
- Adding camera usage description for user permission.
- Creating a data scanner view controller.
- Using delegate methods to react to recognized QR codes.
Let's see how to implement those steps in more detail.
1. Adding camera usage description for user permission
To be able to use the data scanner, the user needs to grant permission for camera usage. We can configure the reason for using the camera by adding the NSCameraUsageDescription key to the target's information property list.
2. Creating a data scanner view controller
When initializing the data scannner, we can specify the item types to scan, quality level, highlighting and more:
let viewController = DataScannerViewController(recognizedDataTypes: [.barcode(symbologies: [.qr])],qualityLevel: .balanced,recognizesMultipleItems: false,isHighFrameRateTrackingEnabled: false,isHighlightingEnabled: true)viewController.delegate = selftry? viewController.startScanning()
As shown in the code above, we pass in [.barcode(symbologies: [.qr])] to scan QR codes. Besides QR codes, the scanner view controller supports various other barcode symbologies. Checkout the official documentation on supported barcode symbologies to learn more.
Besides various barcode types, the data scanner view controller supports text recognition for different text content types like email address, phone number, street address, flight numbers and more.
For example, we could pass in [.text([], .emailAddress)] as recognizedDataTypes to recognize email addresses.
3. Using delegate methods to react to recognized QR codes
The scanner delegate provides methods to react to recognized items or to user's interactions such as tapping on an item. The following delegate method is called when the scanner recognizes new items:
func dataScanner(_ dataScanner: DataScannerViewController, didAdd addedItems: [RecognizedItem], allItems: [RecognizedItem]) {guard let item = allItems.first else { return }switch item {case .barcode(let recognizedCode):print(recognizedCode.payloadStringValue)default:break}}
And that's basically it. The DataScannerViewController makes is really easy to implement QR code scanning functionality in iOS applications.

Newsletter
Like to support my work?
Say hi
Related tags
Articles with related topics
Latest articles and tips