[Huawei Developers] John Wo:
Article Introduction

In this article we will show how to integrate Huawei Scan Kit in iOS using native language (swift) and barcode lookup APIs to search and add items with information in your list. The use case has been created to make Smart Shopping Cart with HMS open capabilities.

Huawei Scan Kit

Huawei Scan Kit supports 13 mainstream formats including QR Code, Data Matrix, PDF417, Aztec, EAN-8, EAN-13, UPC-A, UPC-E, Codabar, Code 39, Code 93, Code 128, and ITF. It detects barcodes despite low visibility, glare, damage, distortion, or blur. It also supports Auto Zoom into small and distant barcodes. It can easily processes up to 5 barcodes simultaneously.

Scan Kit can be called in three ways, from which you can choose as needed.

For this article, we implemented Default View for iOS with native Swift language. In Default View mode, Scan Kit scans barcodes using the camera or from images in the album, and also provides a complete scanning UI that can be directly used. You do not need to worry about designing a UI as Scan Kit provides one.

Pre-Requisites

Before getting started, following are the requirements:

Xcode (During this tutorial, we used latest version 12.3)

iOS 9.0 or later (Scan Kit supports iOS 9.0 and above)

Apple Developer Account

iOS device for testing

Development

Following are the major steps of development for this article:

Step 1: Importing the SDK in Pod Mode

1.1: Check whether Cocoapods has been installed:

gem -v

If not, run the following commands to install Cocoapods:

sudo gem install cocoapods
pod setup

1.2: Run the pod init command in the root directory of the Xcode project and add the current version number to the generated Podfile file.

pod 'ScanKitFrameWork', '~> 1.0.2.300' # Huawei Scan Kit dependency
pod 'Alamofire', '~> 5.2' # Optional: Swift Network call lib dependency
pod 'AlamofireImage', '~> 4.1' # Optional: Swift Image loading lib dependency
pod 'SwiftyJSON', '~> 4.0' # Optional: Swift JSON parsing lib dependency
pod "TTGSnackbar" # Optional: Swift Snackbar lib dependency
pod 'lottie-ios' # Optional: Swift Lottie animation lib dependency

1.3: Run the following command in the same directory of the Podfile file to integrate the HMS Core Scan SDK:

pod install

If you have used Cocoapods, run the following command to update Cocoapods:

pod update

1.4: After the execution is successful, open the project directory, find the .xcworkspace file, and execute it.

Step 2: Assigning Permissions

You need to assign permissions required by your app to call Scan Kit.

To scan barcodes using the camera, you need to add Camera Usage Description (camera permission).

To scan barcodes from images in the album, you need to add Photo Library Usage Description (image read permission).

Open the info.plist file and add the following content:

Step 3: Building Layout

We used Auto Layout. Auto Layout defines your user interface using a series of constraints. Constraints typically represent a relationship between two views. Auto Layout then calculates the size and location of each view based on these constraints. This produces layouts that dynamically respond to both internal and external changes.

In this article, we also used Lottie animation for empty cart list and for the searching animation when user scan barcodes.

// This extension is responsible to manage all the lottie animation methods
extension HomeViewController {
func setDefaultAnimation(){
if(self.addedItems.count > 0){
showResults()
} else {
noItemFound()
}
}

func noItemFound(){
DispatchQueue.main.async {
self.homeTableView.isHidden = true
self.animationView.isHidden = false
self.animationView.animation = Animation.named("empty_view")
self.animationView.contentMode = .scaleAspectFit
self.animationView.loopMode = .loop
self.animationView.animationSpeed = 1.5
self.animationView.play()
}
}

func showLoading(){
DispatchQueue.main.async {
self.homeTableView.isHidden = true
self.animationView.isHidden = false
self.animationView.animation = Animation.named("not_found")
self.animationView.contentMode = .scaleAspectFit
self.animationView.loopMode = .loop
self.animationView.animationSpeed = 1.5
self.animationView.play()
}
}

func showResults(){
DispatchQueue.main.async {
self.homeTableView.isHidden = false
self.animationView.isHidden = true
}
}
}

More information, you can check https://forums.developer.huawei.com/forumPortal/en/topic/0203446609479290092