All tutorials

Using the iOS OCR Component

easy

This guide will help you integrate the what3words OCR Components into your iOS application, enabling users to seamlessly scan and interpret what3words addresses from images, using iOS’ Vision Framework or the what3words OCR SDK.

Features

  • Detects and extracts what3words addresses from images.
  • Supports both live and static image scanning.
  • Flexible APIs to configure scanning preferences.

GitHub Repository

Access the library and detailed documentation on
github icon white iOS OCR Component.

Demo

ocr-component-demo

1Get API key
2Installation

Swift Package Manager

  1. Add the W3W OCR Swift Components to your project in Xcode, and navigate to File > Add Packages.
  2. Enter the package URL: https://github.com/what3words/w3w-swift-components-ocr.git
  3. Select the latest version and integrate it into your project.

Import the library in your Swift files:

import W3WSwiftComponentsOcr
Copied
3Initialisation

Using iOS’ Vision Framework

To use the Vision Framework for OCR, initialize the W3WOcrNative class with the what3words API:

let api = What3WordsV4(apiKey: "YourApiKey")
let ocr = W3WOcrNative(api)
let ocrViewController = W3WOcrViewController(ocr: ocr)
Copied

Using the what3words OCR SDK

For offline functionality, use the what3words OCR SDK by specifying the path to OCR data files and an optional language code:

let ocr = W3WOcr(dataPath: "/path/to/ocr/datafiles", language: "en")
let ocrViewController = W3WOcrViewController(ocr: ocr)
Copied
4Usage

Displaying the OCR Scanner

Use the following example to present the OCR scanner in a UIViewController:

@IBAction func scanButtonPressed(_ sender: Any) {
    let ocrViewController = W3WOcrViewController(ocr: ocr, w3w: api)
    
    // Present the scanner
    present(ocrViewController, animated: true)
    
    // Start scanning
    ocrViewController.start()
    
    // Handle address selection
    ocrViewController.onSuggestionSelected = { suggestion in
        print(suggestion)
        ocrViewController.stop()
        ocrViewController.dismiss(animated: true)
    }
    
    // Handle errors
    ocrViewController.onError = { error in
        print(error)
    }
}
Copied

Customisation

You can customise the appearance of the OCR scanner by applying themes through the W3WThemes framework:

import W3WThemes

let theme = W3WTheme.forOcr(
    backgroundColor: UIColor.white.w3wColor,
    cameraBackgroundColor: UIColor.darkGray.w3wColor,
    headerTextColor: UIColor.red.w3wColor,
    fonts: W3WFonts(font: UIFont(name: "Courier", size: 24.0))
)

let ocrViewController = W3WOcrViewController(ocr: ocr, theme: theme, w3w: api)
Copied

Customisable Properties:

  • Colours: backgroundColor, headerTextColor, etc.
  • Fonts: Customisable with W3WFonts.
  • Line Styles: Modify defaultLineColor, successLineColor, and thickness options.

Camera Permission

Ensure camera permissions are added to your app’s Info.plist:

<key>NSCameraUsageDescription</key>
<string>We need access to your camera to scan what3words addresses.</string>
Copied
5Sample App

The OCR component repository includes a sample app demonstrating the usage of the what3words OCR component.

Mobile AppDetect if text looks like a 3 word addressAndroidSwift

Related tutorials