Walkthrough - Ionic Framework, Barcode Scanner, Android, iOS
Goal
- Build BarCode Scanner application on iOS and Android platforms.
- Use cordova and ionic framework.
- Use windows 8 64bit for Android development.
- Use OSX for iOS development.
- Keep the source code on bitbucket.org as Git. It must be sharable between windows and OSX.
Before Starting…
- Cordova is engine. Phonegap is Adobe’s productized package that wraps Cordova.
- Ionic Framework is comparable to Phonegap. It is built upon Cordova. It lays AngularJS framework for www content and use Angular directive to support ionic-specific directives.
- www folder content is generic across platform.
- When you install platform it contains path information which is OS-specific. (unix style vs DOS style) So you need to decide which OS to use per each platform.
Step 1: Install Android environment on Windows
- Install JDK 7 or later. http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
- Install Android SDK. Let’s say we did on C:\Programs\Android\android-sdk. http://developer.android.com/sdk/index.html?hl=sk
- Execute
C:\Programs\Android\android-sdk\AVD Manager.exe
and create one Android Virtual Device. - Make sure Environment Variables are set properly.
System Variables:
- ANDROID_HOME: C:\Programs\Android\android-sdk\tools
- JAVA_HOME: C:\Program Files\Java\jdk1.7.0_40 (or wherever you installed)
- PATH should include: C:\Programs\Android\android-sdk\platform-tools;C:\Programs\Android\android-sdk\tools;
Step 2: Install Cordova and Ionic Framework on Windows
- Install nodejs for npm. http://nodejs.org/
- Install ant.
- I unzipped http://www.apache.org/dist/ant/binaries/apache-ant-1.9.4-bin.zip under C:\Programs\apache-ant-1.9.4.
- And then added C:\Programs\apache-ant-1.9.4\bin; to PATH.
- Launch Command Prompt and run this. http://ionicframework.com/docs/guide/installation.html
npm install -g cordova gulp ionic plugman
- Build initial app
- Cordova command line interface: http://cordova.apache.org/docs/en/3.4.0/guide_cli_index.md.html#The%20Command-Line%20Interface
C:\Repositories>ionic start MyApp blank
C:\Repositories>cd MyApp
C:\Repositories\MyApp>cordova platform add android
C:\Repositories\MyApp>cordova emulate android
You should see Ionic Blank Starter screen on Android Virtual Device window.
Step 3: Test MyApp on Android device
- Connect Android device to Windows 8 computer on USB port.
- On Android device, enable USB Debugging.
- On Windows 8 computer, run the following.
C:\Repositories\MyApp>cordova emulate android --device
You should see Ionic Blank Starter on your Android device.
Step 4: Add Barcode Scanner plugin for android platform
- Install BarcodeScanner on android platform.
C:\Repositories\MyApp>plugman install --plugins_dir plugins --platform android --project platforms/android --plugin https://github.com/wildabeast/BarcodeScanner
- Modify index.html.
<body ng-app="starter">
<div ng-controller="MainController">
<button id="startScan" class="button button-positive" ng-click="scan()">
Scan
</button>
<div id="results" ng-bind="results" />
</div>
</body>
- Add js/MainController.js
"use strict";
angular.module('starter').controller({
MainController: ['$scope',
function ($scope) {
$scope.results = "result here.";
$scope.scan = function () {
cordova.plugins.barcodeScanner.scan(
function (result) {
$scope.results = JSON.stringify(result);
$scope.$apply();
},
function (error) {
alert("Scanning failed: " + error);
}
);
};
}]
});
- Test
C:\Repositories\MyApp>cordova emulate android --device
You should see Scan button on your Android device.
Step 5: Debugging Android Device from Windows
- Install weinre (WEb INspector REmote).
npm install -g weinre
- Add INTERNET permission on MyApp\platforms\android\AndroidManifest.xml.
<?xml version='1.0' encoding='utf-8'?>
<manifest ...>
...
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
- Launch weinre server.
- Put local IP address using –boundHost.
- If 8080 is occupied, you can change it with –httpPort. * Parameters are case sensitive. If you put incorrect case, it won’t raise error but just ignore the setting.
weinre --httpPort 8081 --boundHost 192.168.0.114
- Add one line on index.html.
<head>
...
<script src="http://192.168.0.114:8081/target/target-script-min.js"></script>
</head>
- Open browser window for http://192.168.0.114:8081/client/.
- Launch the app on Android device. The browser window is linked to the Android device.
Step 6: Commit MyApp to BitBucket.org from Windows
- Install git. http://git-scm.com/downloads
- Got to BitBucket.org and create a git repo called MyApp.
- Run the following.
C:\Repositories\MyApp>git init
C:\Repositories\MyApp>git add .
C:\Repositories\MyApp>git commit -m "init"
C:\Repositories\MyApp>git remote add origin https://kennethc@bitbucket.org/kennethc/myapp.git
C:\Repositories\MyApp>git push -u origin --all
(Type Your Password)
Step 7: Run it from OSX
- Install git.
- Install nodejs.
- Install cordova, ionic, ios-deploy (launch the app on iOS device), ios-sim (launch the app on iOS simulator), and plugman.
npm install -g cordova ionic ios-deploy ios-sim plugman
- Get the code from BitBucket.
git clone https://kennethc@bitbucket.org/kennethc/myapp.git
-
Open XCode, connect iOS device to OSX machine, and select “Use for development” using XCode Organizer. https://developer.apple.com/library/ios/recipes/xcode_help-devices_organizer/articles/provision_device_for_development-generic.html
-
Add ios platform, and run from iOS device.
cd myapp
cordova platform add ios
cordova emulate ios --device
- Debugging works fine with weinre, but you can also use Safari’s Web Inspector. http://phonegap-tips.com/articles/debugging-ios-phonegap-apps-with-safaris-web-inspector.html
Written on May 12, 2014