How to Make a Barcode Reader App within Only 8 Hours by Stealing This Free Code

Be it a local grocer’s checkout line or assisting an inventory process at a retailer, Barcode reader app is always a handy tool.

As a matter of fact, nowadays, Barcode readers have been made in a way to the consumer for cataloging, shopping, and other purposes. Therefore, we thought why don’t we make one barcode scanner app for iPhone!

Yes, in this iOS app tutorial, we’re going to show the entire process of how to make a barcode scanner app within just 8 hours.

Now luckily for us, Apple has already made the process of barcode scanning easy-to-implement.


They have basically provided AV foundation framework in order to help us create a barcode scanner app. Now it goes without saying that a simple barcode scanning app will only work with a camera-equipped device. So grab your camera-equipped Apple device and get started!

Getting Started

👉 Create a new project under the file menu and select “Single View Application”.
👉 In the next tab, name your project that you wish. Here, we’re going to name it as SOQRCodeReader.
👉 Now set outlet of UIView and UIButton in “ViewController.swift”

@IBOutlet weak var viewReader: UIView!
@IBOutlet weak var btnStartStop: UIButton!

Connect it to storyboard.

Set IBAction for “btnStartStop”.

@IBAction func startStopReading(sender: AnyObject)

👉 Add AVFoundation in “Linked Frameworks and Libraries”.
👉 Now import it into ViewController.swift
👉 Create object following object in ViewController.swift for camera capture and get metadata of captured Bar.

// Create a session object.
var captureSession = AVCaptureSession()

// Create output object.
var metaDataOutput = AVCaptureMetadataOutput()

var previewLayer = AVCaptureVideoPreviewLayer()
var captureDevice : AVCaptureDevice?

var isSessionStart = false

👉 Set delegate AVCaptureMetadataOutputObjectsDelegate for ViewController.swift

class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate

👉 Now we’ll create an input object and add it to the AVCaptureSession. Then, add AVCaptureVideoPreviewLayer in viewReader(outlet in storyboard) on click on “Start” button

@IBAction func startStopReading(sender: AnyObject) {
if isSessionStart == false {

captureSession.sessionPreset = AVCaptureSessionPresetHigh

let devices = AVCaptureDevice.devices()

// Set the captureDevice.
for device in devices {
// Make sure this particular device supports video
if (device.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the back camera
if(device.position == AVCaptureDevicePosition.Back) {
captureDevice = device as? AVCaptureDevice

if captureDevice != nil {

// Create input object.
let input : AVCaptureDeviceInput = try! AVCaptureDeviceInput(device: captureDevice)

if captureSession.canAddOutput(metaDataOutput) && captureSession.canAddInput(input)
print("adding out put to session ")
// Add input to the session.

// Add output to the session.

let metadataQueue = dispatch_queue_create("com.mainqueue.reder", nil);

// Send captured data to the delegate object via a serial queue.
metaDataOutput.setMetadataObjectsDelegate( self, queue: metadataQueue)

// Set barcode type for which to scan: EAN-13.
metaDataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code]

//// Add previewLayer and have it show the video data.
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

let bounds:CGRect = self.viewReader.layer.bounds
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.bounds = bounds
previewLayer.position = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds))

viewReader.hidden = false
btnStartStop.setTitle("Stop", forState: .Normal)
print("array \(metaDataOutput.metadataObjectTypes)")
btnStartStop.setTitle("Start", forState: .Normal)
print("no device found")
self.alert("no device found")
} else {
btnStartStop.setTitle("Start", forState: .Normal)

isSessionStart = !isSessionStart

👉 Now integrate AVCaptureMetadataOutputObjectsDelegate for output/metadata of BarCode read by AVCaptureSession

//MARK:- AVCaptureMetadataOutputObjectsDelegate
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]?, fromConnection connection: AVCaptureConnection!) {
print("processing output")

var barCodeObject : AVMetadataObject!
var strDetected : String!

//All the bar code types defined here
let barCodeTypes = [AVMetadataObjectTypeFace,

// The scanner is capable of capturing multiple 2-dimensional barcodes in one scan.
// Get the object from the metadataObjects array.
for metadata in metadataObjects! {

for barcodeType in barCodeTypes {

if metadata.type == barcodeType {
barCodeObject = self.previewLayer.transformedMetadataObjectForMetadataObject(metadata as! AVMetadataMachineReadableCodeObject)
strDetected = (metadata as! AVMetadataMachineReadableCodeObject).stringValue


func alert(setMessage: String){
let alert : UIAlertController = UIAlertController(title: "BarCode", message: "\(setMessage)", preferredStyle: .Alert)
let actionOK:UIAlertAction = UIAlertAction(title: "OK", style: .Default) { (action) in
self.presentViewController(alert, animated: true, completion: nil)

👉 And we are done!

Now when you launch this demo, it should look like below:


And once you scan any barcode, you’ll get an output like below:


Bottom line is – barcode scanner mobile app is very useful for startups as well as for big businesses, especially for physical product selling businesses.

And for this reason, our developers have already experience in working on such barcode reader apps.

Now if you think that there are already many free barcode scanner apps available to use for your business, it’s true. They are indeed available, but you might face problems adjusting them according to your business.

Therefore, it is more reliable and wise to develop a personal barcode scanner app specifically suited for your own business. And in case, you don’t know how to develop – you can always hire iPhone app developer from us to work on your project.

We’re an award winning iPhone app development company in India with 5+ years of experience. Throughout our journey, we’ve worked with 25+ successful startups and developed over 2000 mobile apps for our clients. This means not only we’ve hands-on experience in this industry, but we know each and every aspect of developing a successful mobile app.

You can grab a free copy of barcode scanner tutorial from Github.

Author Bio
Hitesh Trivedi
Hitesh Trivedi
Designation: iOS Team Lead

Hitesh Trivedi is an iOS Team Lead at Space-O Technologies. He has over 10 years of experience in iOS app development. He has guided to develop over 100 iPhone apps with unique features and functionalities. He has special expertise in Swift and Objective-C.