Scanbot SDK has been acquired by Apryse! Learn more

Learn more
Skip to content

How to build a Number Scanner app for iOS in Swift

Kevin July 8, 2025 5 mins read
iOS Data Capture SDK

In this tutorial, we’ll use Xcode, Swift, and the Scanbot Text Pattern Scanner SDK to create an iOS app for extracting sequences of digits from a live camera stream.

Extracting a serial number from a live camera stream with our iOS Number Scanner app

We’ll achieve this in just four easy steps:

  1. Preparing the project
  2. Setting up the main screen
  3. Implementing the number scanning feature
  4. Setting the desired string using regular expressions

All you need is a Mac with the latest version of Xcode and a test device, since we’ll need to use the camera.

Want to see the final code right away? Click here.

ViewController.swift:

import UIKit
import ScanbotSDK

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func scanNumberButtonTapped(_ sender: Any) {
        let configuration = SBSDKUI2TextPatternScannerScreenConfiguration()

        let patternValidator = SBSDKPatternContentValidator(allowedCharacters: "0123456789",
                                                            pattern: "^[0-9]{11}$",
                                                            matchSubstring: false,
                                                            patternGrammar: .regex)
        configuration.scannerConfiguration.validator = patternValidator

        SBSDKUI2TextPatternScannerViewController.present(on: self,
                                                         configuration: configuration) { result in
            if let result {
                var message = result.rawText
                let alert = UIAlertController(title: "Result",
                                              message: message,
                                              preferredStyle: .alert)
                let action = UIAlertAction(title: "OK",
                                           style: .cancel)
                alert.addAction(action)
                self.present(alert, animated: true)
            } else {
                let alert = UIAlertController(
                    title: "No Result",
                    message: "Scanning process canceled by the user.",
                    preferredStyle: .alert
                )
                let action = UIAlertAction(title: "OK", style: .cancel)
                alert.addAction(action)
                self.present(alert, animated: true)
            }
        }
    }
}

Step 1: Prepare the project

Open Xcode and create a new iOS App project. Name the project (e.g., “iOS Number Scanner”), choose Storyboard as the interface, and Swift as the language.

After opening your project, go to File > Add Package Dependencies… and add the Scanbot SDK package for the Swift Package Manager.

Open your Main App Target’s Info tab and add a Privacy – Camera Usage Description key with a value such as “Grant camera access to start scanning”.

In AppDelegate.swift, add a line for setting the license key to the application(_:didFinishLaunchingWithOptions:) method. We don’t need one for this tutorial, but if you have a license key you’d like to use, uncomment the code below and replace <YOUR_LICENSE_KEY> with your actual key.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Uncomment the following line and replace <YOUR_LICENSE_KEY> with your actual key
    // Scanbot.setLicense("<YOUR_LICENSE_KEY>")

    return true
}

Without a license key, the SDK will run in trial mode for 60 seconds per session. If you need more than that, you can generate a free trial license.

Step 2: Set up the main screen

We’ll now create a simple UI for starting our Number Scanner.

First, open Main.storyboard and add a UIButton with a meaningful label (e.g., “Scan Number”) to the main view.

After that, create an IBAction for the button in ViewController.swift and implement the button action to present the scanner view controller.

@IBAction func scanNumberButtonTapped(_ sender: UIButton) {
    // Code to present the scanner view controller will go here
}

We’ll also need to add the following import to ViewController.swift:

import ScanbotSDK

Step 3: Implement the number scanning feature

This step involves presenting the Text Pattern Scanner view controller. The Scanbot SDK provides a pre-built UI for the scanning process.

Add the following code inside scanNumberButtonTapped() in ViewController.swift:

@IBAction func scanNumberButtonTapped(_ sender: Any) {
    let configuration = SBSDKUI2TextPatternScannerScreenConfiguration()
    SBSDKUI2TextPatternScannerViewController.present(on: self,
                                                     configuration: configuration) { result in
        if let result {
            var message = result.rawText
            let alert = UIAlertController(title: "Result",
                                          message: message,
                                          preferredStyle: .alert)
            let action = UIAlertAction(title: "OK",
                                       style: .cancel)
            alert.addAction(action)
            self.present(alert, animated: true)
        } else {
            // Indicates that the cancel button was tapped.
        }
    }

You can also handle the user canceling the scanning process like this:

} else {
    let alert = UIAlertController(
        title: "No Result",
        message: "Scanning process canceled by the user.",
        preferredStyle: .alert
    )
    let action = UIAlertAction(title: "OK", style: .cancel)
    alert.addAction(action)
    self.present(alert, animated: true)
}

Step 4: Set the desired string using regular expressions

As it is now, our scanner will scan any string of characters. This can be useful, but if you only want to extract specific kinds of data, you’ll need to pre-define the string structure in advance.

For example, if we know that we’re looking for a serial number that is 11 digits long, we can configure the scanner like this:

@IBAction func scanNumberButtonTapped(_ sender: Any) {
    let configuration = SBSDKUI2TextPatternScannerScreenConfiguration()

    let patternValidator = SBSDKPatternContentValidator(allowedCharacters: "0123456789",
                                                        pattern: "^[0-9]{11}$",
                                                        matchSubstring: false,
                                                        patternGrammar: .regex)
    configuration.scannerConfiguration.validator = patternValidator

    // existing code

Using regular expressions allows us to pre-define exactly what kind of character sequences we’re looking for. This way, we can ensure that only data structures adhering to that pattern are extracted.

Your final ViewController.swift will then look like this:

import UIKit
import ScanbotSDK

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func scanNumberButtonTapped(_ sender: Any) {
        let configuration = SBSDKUI2TextPatternScannerScreenConfiguration()

        let patternValidator = SBSDKPatternContentValidator(allowedCharacters: "0123456789",
                                                            pattern: "^[0-9]{11}$",
                                                            matchSubstring: false,
                                                            patternGrammar: .regex)
        configuration.scannerConfiguration.validator = patternValidator

        SBSDKUI2TextPatternScannerViewController.present(on: self,
                                                         configuration: configuration) { result in
            if let result {
                var message = result.rawText
                let alert = UIAlertController(title: "Result",
                                              message: message,
                                              preferredStyle: .alert)
                let action = UIAlertAction(title: "OK",
                                           style: .cancel)
                alert.addAction(action)
                self.present(alert, animated: true)
            } else {
                let alert = UIAlertController(
                    title: "No Result",
                    message: "Scanning process canceled by the user.",
                    preferredStyle: .alert
                )
                let action = UIAlertAction(title: "OK", style: .cancel)
                alert.addAction(action)
                self.present(alert, animated: true)
            }
        }
    }
}

Our Number Scanner iOS app is now ready. Build and run the app and scan a serial number …

Example of a serial number

… to test your scanner!

Extracting a serial number from a live camera stream with our iOS Number Scanner app

Conclusion

This completes the basic implementation of a Number Scanner for iOS using Swift and the Scanbot SDK. We used the SDK’s Ready-to-Use UI Components with their default values for this tutorial, but you can further customize the text pattern scanning screen to fit your needs.

For more information on how to configure the Text Pattern Scanner module, head over to the documentation.

Should you have questions or run into any issues, we’re happy to help! Just shoot us an email via tutorial-support@scanbot.io.

Happy scanning! 🤳

Related blog posts