How to scan documents in under 10 lines of code

Thanks to VisionKit framework scanning documents is very easy with iOS 13 and up. In this blog post I will show you how.

Published: June 7, 2020
App Store

Start by importing the framework:

import VisionKit

We are going to use VNDocumentCameraViewController which is ready-made controller available for us to use 🙂

Basic usage

The basic usage looks like this:

func displayScanningController() {
    guard VNDocumentCameraViewController.isSupported else { return }

    let controller = VNDocumentCameraViewController()
    controller.delegate = self

    present(controller, animated: true)
}

Just to be safe we first check if this class is supported. The next part is just creating instance, setting delegate and using present to show this class from our ViewController.

Delegate methods

The next part is to implement VNDocumentCameraViewControllerDelegate which requires two methods:

extension ViewController: VNDocumentCameraViewControllerDelegate {
    func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
    // save scan
        dismiss(animated: true, completion: nil)
    }
    func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) {
        dismiss(animated: true, completion: nil)
    }
}

The didFinishWith will give us instance of VNDocumentCameraScan which contains the scanned pages. In both cases we need to dismiss the controller.

And the last part is saving the data 🙂

Accessing scanned images

The VNDocumentCameraScan has pageCount property indicating how many pages user scanned and method imageOfPage to get UIImage for specified page.

We can use for in loop to get all the scanned pages and save them for example:

for index in 0 ..< scan.pageCount {
    let image = scan.imageOfPage(at: index)
     // save image
}

These images will likely be quite big, if you concerned about size you can do some resizing or get the JPEG data first and convert them back to UIImage.

You can also try accessing the title property of the scan to maybe suggest it to the user.

And that is scanning in iOS 13+ in a nutshell.

Of course this is not just for building document scanner apps. It is perfectly suited for apps for notes (Apple Notes uses this same controller) or maybe for business chat apps where users can quickly send scanned documents without going to another app and sharing it to chat..

Uses: Xcode 12 & Swift 5.3

Filip Němeček profile photo

WRITTEN BY

Filip Němeček @nemecek_f@iosdev.space

iOS blogger and developer with interest in Python/Django. Want to see most recent projects? 👀

iOS blogger and developer with interest in Python/Django. Want to see most recent projects? 👀