WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

How to detect documents using VNDocumentCameraViewController

Swift version: 5.10

Paul Hudson    @twostraws   

iOS 13.0 introduced a new micro-framework called VisionKit, which is specifically designed to make it possible to scan documents like Notes does.

You can then Vision OCR to scan the text if you want, but by default VNDocumentCameraViewController just gives you images of each page.

To get started:

  1. Import VisionKit.
  2. Make some type (such as your view controller) conform to the VNDocumentCameraViewControllerDelegate protocol so you can handle delegate callbacks.
  3. Create and present an instance of VNDocumentCameraViewController, setting its delegate property to whatever should be notified when a scan completes.
  4. Present the document scanner as normal, then wait for feedback.

So, something like this:

let vc = VNDocumentCameraViewController()
vc.delegate = self
present(vc, animated: true)

Once the scan completes your delegate will get called with the document, like this:

func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
    print("Found \(scan.pageCount)")

    for i in 0 ..< scan.pageCount {
        let img = scan.imageOfPage(at: i)
        // ... your code here

The result of imageOfPage(at:) is a UIImage, so you’ll need to replace “your code here” with whatever you want to do with your images.

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Available from iOS 13.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.1/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.