Chariot Day 3/2/13
Don Coleman
Chariot Solutions
@doncoleman
PhoneGap is an open source solution for building cross-platform mobile apps with standards-based Web technologies like HTML, JavaScript, CSS.
PhoneGap Supports Many Platforms
iOS, Android
Windows Phone, Blackberry
Symbian, Bada, WebOS
Tizen, FirefoxOS
PhoneGap embeds a web view in a native app
JavaScript API
Your app is a native app
PhoneGap does not provide a UI
Can I wrap any website in PhoneGap and magically have a mobile app?
Single Page App
Unzip archive
move somewhere like /usr/local/phonegap-2.5.0
Do you have platform tools installed?
Xcode
Android SDK
Visual Studio
BlackBerry Webworks SDK
iOS
$ cd /usr/local/phonegap-2.5.0
$ cd lib/ios/bin
$ ./create ~/foo com.example.foo Foo
$ cd ~/foo
$ ./cordova/emulate
$ cd /usr/local/phonegap-2.5.0
$ cd lib/android/bin
$ ./create ~/foo com.example.foo Foo
$ cd ~/foo
$ ./cordova/run
PhoneGap Getting Started Guides
http://docs.phonegap.com/en/2.5.0/guide_getting-started_index.md.htmlindex.html
<!DOCTYPE html>
<html>
...
<script src="cordova-2.5.0.js"></script>
<script src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
</html>
index.js
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener(
'deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
// your code here
}
};
navigator.camera.getPicture(
cameraSuccess, cameraError, [ cameraOptions ] );
http://docs.phonegap.com/en/2.5.0/cordova_camera_camera.md.html
API Call
navigator.camera.getPicture(
onSuccess,
onFailure,
{
quality: 50,
destinationType: Camera.DestinationType.FILE_URI
}
);
Callbacks
function onSuccess(imageURI) {
image.src = imageURI;
}
function onFailure(message) {
alert('Failed because: ' + message);
}
Permissions
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
index.html
<div>
<div>
<img id="image"></img>
<div>
<div>
<h1 id="cameraLabel">Camera</h1>
<div>
</div>
index.js
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener(
'deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
cameraLabel.addEventListener(
'touchstart', app.takePicture, false);
},
takePicture: function() {
// ...
}
};
index.js
takePicture: function() {
navigator.camera.getPicture(
function(imageURI) { // success
image.src = imageURI;
},
function(error) { // failure
console.log("error");
},
{ // options
quality: 50,
destinationType: DestinationType.FILE_URI,
targetWidth: 320,
targetHeight: 320,
allowEdit: true,
saveToPhotoAlbum: false
}
);
}
Lots of other APIs
http://docs.phonegap.com/en/2.5.0/index.htmlBut PhoneGap doesn't do
[insert your feature here]
Extend PhoneGap
PhoneGap is built out of plugins
https://github.com/chariotsolutions/phonegap-nfc
Android, BlackBerry 7, BlackBerry 10, Windows Phone 8
Clone git repo
Copy Java files to project
Copy JavaScript files to project
Edit config.xml
Edit AndroidManifest.xml
Edit index.html
Plugin Specification
https://github.com/alunny/cordova-plugin-specA command line tool to distribute and package plugins for use with Apache Cordova
https://github.com/imhotep/plugman
$ plugman
Usage
---------
Add a plugin:
plugman --platform --project --plugin
Remove a plugin:
plugman --remove --platform --project --plugin
List plugins:
plugman --list
$ plugman --list
BarcodeScanner - Cross-platform BarcodeScanner
ChildBrowser - ChildBrowser plugin
Facebook-Connect - Official plugin for Facebook Connect
GAPlugin - Google Analytics Plugin
KeychainPlugin - Keychain Plugin for Apache Cordova
NFC - Near Field Communication Plugin
PushPlugin - Push Notification Plugin for iOS & Android
TestflightPlugin - TestFlight Plugin for Apache Cordova
plugman --platform android --project . --plugin NFC
Create a project
$ cd /usr/local/phonegap-2.5.0/lib/android/bin
$ ./create /tmp/nfc com.example.nfc Nfc
Install PhoneGap NFC
$ cd /tmp/nfc
$ plugman --platform android --project . --plugin NFC
index.html
<script type="text/javascript" src="js/phonegap-nfc.js">
</script>
index.js
deviceready: function () {
nfc.addNdefListener(app.onNfc);
}
index.js
onNfc: function(nfcEvent) {
var tag = nfcEvent.tag,
records = tag.ndefMessage,
payload,
message;
if (records[0]) {
payload = records[0].payload;
message = nfc.bytesToString(payload);
navigator.notification.alert(message, null, "NFC");
}
}
read the log output
ensure code is valid with jshint
Other ways to debug
npm install cordova -g
Install and debug iPhone apps without using Xcode
https://github.com/filmaj/fruitstrapContinuous integration setup for Apache Cordova
https://github.com/filmaj/medicFil Maj will be speaking about
cordova-cli and medic at ETE
Don Coleman