How to Generate QR Codes and Barcodes in an iOS App (Swift)

In this article, we will explain how to generate and display QR codes and barcodes in an iOS app using Swift.

How to Generate QR Code and Barcode in iOS App with Swift 1

Prepare an iOS App to Display QR Codes and Barcodes

First, let’s create a simple iOS app to display QR codes and barcodes for testing purposes.

In Xcode, create a new project by selecting [iOS] > [App].

For the design, simply add one Image View to the View Controller on the Main storyboard.

How to Generate QR Code and Barcode in iOS App with Swift 2


Create an outlet from that Image View and name it codeImageView.

How to Generate QR Code and Barcode in iOS App with Swift 3

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var codeImageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

If you are not familiar with how to add buttons or create actions in Xcode, please refer to How to Create a Basic iOS App.


How to Generate and Display a QR Code in Swift

Now, let’s write the code to generate and display a QR code using Swift.

Modify the viewDidLoad() in ViewController.swift as follows:

override func viewDidLoad() {
    super.viewDidLoad()
    
    let codeValue = "1234567890"
    
    let codeData = codeValue.data(using: .ascii)
    if let ciFilter = CIFilter(name: "CIQRCodeGenerator") {
        ciFilter.setValue(codeData, forKey: "inputMessage")
        if let codeImage = ciFilter.outputImage  {
            codeImageView.image = UIImage(ciImage: codeImage)
        }
    }
}

How to Generate QR Code and Barcode in iOS App with Swift 4


On line 4, we define a string (codeValue) to be displayed as a QR code.

On line 6, the string is converted into Data using ASCII encoding and assigned to codeData.


On line 7, we create a CIFilter object, which is used for image processing and generation.

The initializer of CIFilter requires the type of filter to use. In this case, we specify CIQRCodeGenerator to generate a QR code.


With CIFilter, required parameters are passed as key-value pairs.

For CIQRCodeGenerator, the data to encode into the QR code is passed using the key inputMessage.

How to Generate QR Code and Barcode in iOS App with Swift 5

On line 8, we set codeData as the value for the inputMessage key of the filter.


On line 9, we get the processed CIImage object from the outputImage property of the filter.

On line 10, we assign the generated QR code image to the image property of codeImageView to display it.


That’s it—you now have working code to generate and display a QR code in Swift.


When you run this app on the simulator, it will display a QR code like this:

How to Generate QR Code and Barcode in iOS App with Swift 6

If you scan this QR code with the iPhone camera app, it will display the value 1234567890.


How to Generate and Display a Barcode in Swift

The process to generate a barcode in Swift is almost the same as generating a QR code.

The difference is that instead of CIQRCodeGenerator, we use CICode128BarcodeGenerator when creating the CIFilter object.

CICode128BarcodeGenerator is a filter for generating barcodes in the CODE128 format.

Just like with the QR code generator, the data to encode is passed using the inputMessage key. So you can simply replace the filter name in the previous code to generate a barcode.

override func viewDidLoad() {
    super.viewDidLoad()
    
    let codeValue = "1234567890"
    
    let codeData = codeValue.data(using: .ascii)
    if let ciFilter = CIFilter(name: "CICode128BarcodeGenerator") {
        ciFilter.setValue(codeData, forKey: "inputMessage")
        if let codeImage = ciFilter.outputImage  {
            codeImageView.image = UIImage(ciImage: codeImage)
        }
    }
}

When you run this app on the simulator, a barcode will be generated and displayed like this:

How to Generate QR Code and Barcode in iOS App with Swift 7


That's how to generate and display QR codes and barcodes in an iOS app using Swift.