On this page

Stuck? Open a support ticket and the barKoder team will help. Open a ticket

NativeScript Installation

Before you start #

Environment Setup for NativeScript #

Follow the official NativeScript setup guide to install all required tools and dependencies for your operating system.

Make the app with the NativeScript CLI (ns, from the setup guide) and its TypeScript template; the code below goes into its app/main-page.xml, app/main-page.ts and app/main-view-model.ts:

Shell
ns create BarkoderApp --ts
cd BarkoderApp

1. Add the package #

Add the package #

Install the package from npm, or from a folder you downloaded.

Option A – Install from npm #

In the app's folder:

Shell
npm i barkoder-nativescript

Option B – Install from a Local Package #

  • Download the package from Downloads in the barKoder Developer Portal.
  • Unzip it.
  • Add it to your project by its path:
Shell
tns plugin add <path to barkoder-nativescript package>

Type checking #

The package comes as TypeScript sources, which ns build type-checks with your app; it stops on errors in the package's own files (barkoder-nativescript 1.7.2). To report only the errors in your own code, add the chainWebpack lines to webpack.config.js:

JavaScript
const webpack = require("@nativescript/webpack");

module.exports = (env) => {
	webpack.init(env);

	// barkoder-nativescript comes as TypeScript sources: type-check only your own files
	webpack.chainWebpack((config) => {
		config.plugin('ForkTsCheckerWebpackPlugin').tap(([options]) => [{ ...options, issue: { exclude: [{ file: '**/node_modules/**' }] } }]);
	});

	return webpack.resolveConfig();
};

Import package #

Import the constants where you use them:

TypeScript
import { BarkoderConstants } from 'barkoder-nativescript/barkoder-nativescript.common';

2. Allow the camera #

Camera permissions #

The scanner needs the camera. On Android the plugin adds the permission to the manifest for you. On iOS, add the camera key to App_Resources/iOS/Info.plist:

XML
<key>NSCameraUsageDescription</key>
 <string>Camera permission</string>

3. Add the scanner view #

BarkoderView #

In app/main-page.xml, declare the plugin's namespace on the Page (xmlns:Barkoder: the module that exports BarkoderView) and put the view in your layout:

XML
<Page xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:Barkoder="barkoder-nativescript/barkoder-nativescript.common" navigatingTo="navigatingTo">
   ...

   <Barkoder:BarkoderView id="barkoderView"  class="scanner-container" width="100%" height="70%"/>

   ...
</Page>

Find the view in the view model #

In app/main-view-model.ts (this class replaces the template's HelloWorldModel), find the BarkoderView of the layout and set the license key:

TypeScript
export class HelloWorldModel extends Observable implements BarkoderConstants.BarkoderResultCallback {
private barkoderView : any
constructor(args) {
 super()

 this.barkoderView = args.object.page.getViewById('barkoderView');
 this.barkoderView.setLicenseKey("License_Key")

}
scanningFinished(results: any[], thumbnails: any[], resultImage: any): void {
 console.log(`${results[0].textualData}`)
 console.log(`${results[0].barcodeTypeName}`)
}
}

The constructor takes the page's navigatingTo event; in app/main-page.ts, pass it on:

TypeScript
import { EventData, Page } from '@nativescript/core'
import { HelloWorldModel } from './main-view-model'

export function navigatingTo(args: EventData) {
  const page = <Page>args.object
  page.bindingContext = new HelloWorldModel(args)
}

4. Set the license key and barcode types #

Licensing #

Without a valid license key the SDK still scans, but each result starts with (Unlicensed) and part of its data is replaced with dots. Get a free trial key on the trial page: choose Native or WASM and enter your app ID (for the Web SDK, your domain). A trial key lasts 30 days, for up to 25 devices, one per app ID. For a production license, request a quote. By using the SDK you agree to the End User License Agreement.

Your key goes in setLicenseKey, in the view model's constructor (step 3).

Turn on the barcode types you scan, in the constructor after setLicenseKey (the SDK starts with every type off):

TypeScript
this.barkoderView.setBarcodeTypeEnabled([
  BarkoderConstants.DecoderType.QR,
  BarkoderConstants.DecoderType.Code128
])

5. Start scanning and read results #

Start scanning #

Start scanning at the end of the view model's constructor (the plugin waits until the view is ready) or from a button's tap handler. Each result comes to scanningFinished:

TypeScript
this.barkoderView.startScanning(this)

The General example has the whole app.

Build and run the app on a device or an emulator (ns run ios for iOS):

Shell
ns run android