Logo for tanaschita.com

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.

Sponsorship logo
Capture HTTP(s) traffic with Proxyman
Proxyman - Your ultimate man-in-the-middle proxy to effortlessly capture, inspect, and manipulate HTTP(s) traffic on macOS, Windows, iOS, and Android devices.
Get started for free

Overview

To implement a data scanner for QR codes, the following steps are required:

  1. Adding camera usage description for user permission.
  2. Creating a data scanner view controller.
  3. 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 = self
try? 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.

Sponsorship logo
Capture HTTP(s) traffic with Proxyman
Proxyman - Your ultimate man-in-the-middle proxy to effortlessly capture, inspect, and manipulate HTTP(s) traffic on macOS, Windows, iOS, and Android devices.
Get started for free

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

machine learning

swift

ios

Quick tip on how to split text into words with the Natural Language framework in Swift.

Learn how the tokenizer of the Natural Language framework works.

19 Jun 2023 · 2 min read

Latest articles and tips

© 2023 tanaschita.com

Privacy policy

Impressum