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.

Start by importing the framework:

import VisionKit

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

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.

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 🙂

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
}

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 11 & Swift 5