Skip to content

xremix/SwiftGS1Barcode

Repository files navigation

SwiftGS1Barcode

A GS1 Barcode Library and Parser written in Swift

Badge w/ Version Language iOS macOS Linux Code Coverage GitHub license Build Status

A Library to parse GS1 Barcode strings into a object and allows an easy access to the properties that a GS1 Barcode can have.
Supported is a large set of common Application Identifiers (GS1 Barcodes), but it can be easily extended on the fly to support any identifier needed.

Contributions are most welcome.

You can also find this project on CocoaPods or can use Swift Package Manager.

CocoaPods currently might be out of date. Please compare with latest version of the git tags.

Getting started

Parsing is as simple as

import SwiftGS1Barcode
// ...
let gs1Barcode = "01101234670417283002\u{1D}1721103110S123456"
let barcode = GS1Barcode(raw: gs1Barcode)
barcode.validate() // To check if you barcode is valid

print(barcode.gtin) // 10123467041728
print(barcode.countOfItems) // 2
print(barcode.expirationDate) // 31.10.2021
print(barcode.lotNumber) // S123456

Advanced Usage

To seperate the parsing from initializing I'd recommend a code like

import SwiftGS1Barcode
// ...
let gs1BarcodeText = "01101234670417283002\u{1D}1721103110S123456"
let barcode = GS1Barcode()
barcode.raw = gs1BarcodeText
_ = barcode.parse()

To parse custom Application Identifiers use the following code

import SwiftGS1Barcode
// ...
let gs1BarcodeText = "90HelloWorld\u{1D}01101234670417283002\u{1D}1721103110S123456"
let barcode = GS1Barcode()
barcode.applicationIdentifiers["custom1"] = GS1ApplicationIdentifier("90", length: 30, type: .String, dynamicLength: true)
barcode.raw = gs1BarcodeText
_ = barcode.parse()
print(barcode.applicationIdentifiers["custom1"]!.stringValue)

To see some samples, of how to set up Application Identifiers check out the GS1Barcode Class

Available Properties

The following properties are currently supported:

ID Application Identifier Experimental Support
00 serialShippingContainerCode No
01 gtin No
02 gtinOfContainedTradeItems No
10 lotNumber No
11 productionDate No
12 dueDate No
13 packagingDate No
15 bestBeforeDate No
17 expirationDate No
20 productVariant No
21 serialNumber No
22 secondaryDataFields No
30 countOfItems No
37 numberOfUnitsContained No
310 productWeightInKg No
240 additionalProductIdentification No
241 customerPartNumber No
242 madeToOrderVariationNumber No
250 secondarySerialNumber No
251 referenceToSourceEntity No
392 priceSingleMonetaryArea No
393 priceAndISO No
395 pricePerUOM No
422 countryOfOrigin No
714 nhrnAIM No

Experimental Support means that these are getting parsed, but there are no getter for this. You can get the value by calling e.g. myGs1Barcode.applicationIdentifiers["additionalProductIdentification"]. Also the implementation can change if any parsing issues come up.

You can add custom application identifiers by adding them to the key / value dictionary:

let barcode = GS1Barcode()
barcode.applicationIdentifiers["custom1"] = GS1ApplicationIdentifier("90", length: 30, type: .String, dynamicLength: true)

They'll automatically get parsed by the parse() function.
You can also simply contribute by yourself and add them to the GS1BarcodeParser.swift class, or open an issue if there is something missing for you.

Installation

Swift Package Manager (recommended)

Open your project and Xcode and click File -> Swift Packages -> Add Package Dependency and enter git@github.com:xremix/SwiftGS1Barcode.git

CocoaPods

You can install the library to your project by using CocoaPods. Add the following code to your Podfile:

platform :ios, '8.0'
use_frameworks!

target 'MyApp' do
	pod 'SwiftGS1Barcode'
end

Alternative you can also add the direct Github source (or a different branch):

platform :ios, '8.0'
use_frameworks!

target 'MyApp' do
	pod 'SwiftGS1Barcode', :git => 'https://github.com/xremix/SwiftGS1Barcode', :branch => 'master'
end

Manually

You can add the project as a git submodule. Simply drag the SwiftGS1Barcode.xcodeproj file into your Xcode project.
Don't forget to add the framework in your application target

Resources

A couple of resources, used for this project.

GS1 parsing

https://www.gs1.org/docs/barcodes/GS1_General_Specifications.pdf https://www.activebarcode.de/codes/ean128_ucc128_ai.html https://www.gs1.at/fileadmin/user_upload/Liste_GS1_Austria_Application_Identifier.pdf

CocoaPod

https://www.appcoda.com/cocoapods-making-guide/

Analytics