Ionic 5 Barcode Scanner Plug-in Sample App

In this step-by-step tutorial, we'll demonstrate how to use Appery.io BarcodeScanner plug-in in your Ionic 5 app.

Introduction

This document explains in detail how to build an app that launches a barcode scanner on a button click. After the scan is completed, the scan information will be displayed.

Before You Start

  • Tutorial level: beginner; your first mobile app.
  • Prerequisites: an Appery.io account.

Downloads and Resources

You can also try it yourself by creating from the backup. In this case, follow the steps:

  1. Download the app backup file.
  2. Click Create new app.
  3. Click From backup and select the project backup file on your drive.
  4. Type in the app name.
  5. Click Create:
Creating app from the backupCreating app from the backup

Creating app from the backup

Also, if you are interested in the detailed tutorial, please follow the steps below.

Creating App

  1. From the Apps tab, click Create new app.
  2. Select Ionic 5 > Ionic 5 Blank for the application type, enter Ionic 5 BarcodeScannerApp for the app name, and click Create:

Creating App UI

  1. Every new app comes with a default Screen1 page. Let's open it: Pages > Screen1 and under the page PROPERTIES panel, set the Page Footer property to False as we won't need the bottom app toolbar:
  1. Click the Toolbar title of the screen header and change its Text property to Barcode Scanner App:
  1. Drag and drop the Button component from PALETTE to the page and change its Text property to Scan:
  1. Next, place a Text component under the button and set its Container property to p. Then select the created component and click on the small cog icon in the upper right corner to open the Context menu. Select Clone and create 2 more copies of the component:
  1. For the three resulting text components, replace their default Text properties with:
  • Text: {{text}} for the first component,
  • Format: {{barcodeFormat}} for the second,
  • and Cancelled: {{cancelled}} for the last one:

🚧

It's also a good practice to give the components unique names, so you can replace the generated text _copy name with the name you like.

The resulting UI is simple:

Defining App Logic

  1. Go to the Screen1 CODE panel and under the Variables section, create three new variables: text, barcodeFormat and cancelled, all of String type:
  1. Now, click CREATE NEW > From plugin and under the Appery.io Examples category, select the BarcodeScanner plug-in. After that, click Import selected plugins:

You should now see the BarcodeScannerService and BarcodeScanner_impl entries on the Services and TypeScript lists of the Project view:

  1. Now, switch to the navigate to the Screen1 DATA panel. Here, add the imported service as a datasource. Change its local name to scannerService:
  1. Then, click the Mapping button next to the Success event and create the following mapping. After editing, click the Save & Replace button. Here:
  • text is the text representation of the barcode data.
  • format is the type of barcode detected.
  • cancelled indicates whether or not the scan has been cancelled by the user.
  1. Return to the DESIGN panel to modify the Scan button behavior. Click on the Scan button should trigger the scan. So, unfold the EVENTS tab panel from the bottom and for the Button1 component, create a new Click event. Select Invoke service as the Action and scannerService as the Datasource. Save the event:

Error Handling

Let's add an alert that will be shown in case of any scanning errors. Go to the DATA panel tab and click Add to the right of the Error event of the scannerService. Select Present alert for the action and provide some details: Error as the alert Header and Barcode scanning failed, please try again as its Message. Don’t forget to save all the changes:

App Testing

Since we are using a native component (barcode scanner), the app cannot be tested in a browser. To test the app, you need to build (.apk / .aab for Android or .ipa for iOS) and install it on your device.

👍

App Export

You can check below for how to export your app:

Also, you will need to allow the app to take photos, record videos and access photos, media, and files:

App work on deviceApp work on device

App work on device

Once you get the value from the scan, you can save it in your local storage and use it as an input to another API to get more details about the scan. For example, if you have a product barcode number scanned, you can then invoke a service to get the product details.

📘

Appery.io Video Tutorials

You might also like several Appery.io YouTube videos showing how to:

  • create an Appery.io mobile application for scanning QR codes:

or

  • create an Appery.io mobile application with a QR generator:

Did this page help you?