DEV Community

Filip Němeček
Filip Němeček

Posted on • Edited on

iOS: 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
Enter fullscreen mode Exit fullscreen mode

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)
    }
Enter fullscreen mode Exit fullscreen mode

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)
    }
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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..

I was so impressed when I discovered VisionKit for the first time that I used it to create my new app called Scan it. It is a free document scanner with automatic iCloud Drive upload. Check it out!

Scan it screenshots

Thanks for reading!

Top comments (6)

Collapse
 
onurhazar profile image
Onur Hazar

Hello, thanks for the article! when you scan more than one image, the name of the save button at bottom right is changed as scanned document name. Is that an iOS bug? Plus, can we limit scanning to a single scan with VNDocumentCameraViewController?

Collapse
 
nemecek_f profile image
Filip Němeček

Hello, you are welcome! :-) I dont think you can limit this, but you can always tell the user in advance and then use just the first image if their scan more pages. Or maybe let them choose.

Collapse
 
kvvaradha profile image
kvvaradha • Edited

Hi this is really awesome.

Do you have git version of this app. Can we see it. ?

Collapse
 
kvvaradha profile image
kvvaradha

Any reply ?

Collapse
 
nemecek_f profile image
Filip Němeček

Hi, no sorry, this app is not open-source.

Collapse
 
kvvaradha profile image
kvvaradha

Ok howmuch is it?