Generate an EAN-13 Barcode in Swift

Cloudmersive
1 min readJun 18, 2021

EAN barcodes were created for use in Europe but are recognized internationally. Similar to the UPC formats, EAN codes only encode numeric data, which enables an effortless scanning process. The EAN-13 barcode contains 13 digits that represent the country code, product and manufacturer information, and checksum. In the following tutorial, we will demonstrate how you can use an API in Swift to instantly generate an EAN-13 barcode image for your tracking convenience.

To run the API and call the function, input your barcode value into the below example code:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
var semaphore = DispatchSemaphore (value: 0)let parameters = "0=%3C&1=s&2=t&3=r&4=i&5=n&6=g&7=%3E"
let postData = parameters.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/barcode/generate/ean-13")!,timeoutInterval: Double.infinity)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()

And just like that, a PNG file of your EAN-13 barcode will be ready for download. Easy!

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.