There is a lot of buzz about building Hybrid mobile applications. In this blog series we will talk about building Hybrid mobile applications using PhoneGap. For those who are new to hybrid apps let’s review the pros and cons of this approach.
Why Hybrid?
- Hybrid apps lets you develop and deploy apps much quicker to the app store/marketplace, as you do not have to rewrite the code for different platforms. (HTML5 can be used across different platforms for the markup!)
- Web developers can re-use their existing knowledge rather than learning various development environment/languages.
- Transforming an existing web apps into Hybrid mobile app is much simpler than creating native apps for different platforms (Windows Phone, iOS, Android, etc.)
These are few of the reasons why Hybrid apps are a good option. In this blog we will use PhoneGap to build a native/hybrid Windows Phone App.
Why not Hybrid?
- When you need a 3D gaming app or an app that requires very high FPS (frames per second), as Hybrid apps runs in a webview. (It is as good as Web Application)
- When you want to use native API's/gestures specific to a particular platform. In Hybrid app you may not be able to use all the native API's!
- You need the world's best user experience out of an app! Native is the way to go.
What is PhoneGap?
PhoneGap is a free and open source framework that allows you to create mobile apps using standardized web APIs for differnet platforms. You get to create mobile apps using web technologies (HTML,CSS, and JavaScript).
You can check out the Supported Features/API's by PhoneGap.
Setting up the Environment for building Windows Phone Application using PhoneGap:
Requirements:
- App Hub membership for deployment via Marketplace
- Installing Windows Phone SDK + Cordova (And extract its content.) Note:The software underlying PhoneGap is Apache Cordova, hence referred as Cordova at times.
- Extract Phonegap-[ver].zip, and go to lib -> Windows-Phone-8
- Copy CordovaWP8AppFull-x.x.x.zip template to the folder : \My Documents\Visual Studio 2012\Templates\ProjectTemplates\
Setup New Project:
- Open Visual Studio and choose New Project
- Select CordovaWP8AppFull-x.x.x.zip template, and name the project
Project Structure:
- 'www' folder contains Cordova html/js/css resources in your application. Make sure you add in all resources under this folder.
- Make sure you set all resources added to this folder as content, else the resource would not be rendered.
Build the Project:
- Run the solution in the emulator (F5)
- You may test this XAP on a test device
Check out Mihail’s blog which talks about the difference between CordovaWP8AppFull-x.x.x.zip and CordovaWP8AppStandAlone-x.x.x.zip templates which you can find in the PhoneGap folder.
Now we are ready to get started with PhoneGap for Windows Phone 8 using the default template.
Let's try and access some native features using PhoneGap's API's.
For example let's try to find out the Location of the device using the location API's
We can call the native GPS/ geolocation API using the cordova method:
geolocation.getCurrentPosition
- navigator.geolocation.getCurrentPosition(geolocationSuccess,
- [geolocationError],
- [geolocationOptions]);
Permissions required:
- <Capabilities>
- <CapabilityName="ID_CAP_LOCATION" />
- </Capabilities>
You will need to add the respective permissions/capabilities in the WPAppManifest.xml. While you submit your app for deploying it on the marketplace, Microsoft runs a static analyzer against your application to figure out what capabilities does your app requires to run. Microsoft will place a disclaimer on your app on usage of these capabilities. (In fact the app might get rejected by Microsoft if the capabilities found out by their analysis doesn't match the capabilities mentioned by you!)
Another example:
Accessing Device Details using HTML/JS code:
- <!DOCTYPEhtml>
- <html>
- <head>
- <title>Device Properties Sample</title>
- <scripttype="text/javascript"charset="utf-8"src="cordova-2.5.0.js"></script>
- <scripttype="text/javascript"charset="utf-8">
- // Wait for Cordova to load
- //
- document.addEventListener("deviceready", onDeviceReady, false);
- // Cordova is ready
- //
- function onDeviceReady() {
- var element = document.getElementById('deviceProperties');
- element.innerHTML = 'Device Name: ' + device.model + '<br />' +
- 'Device Cordova: ' + device.cordova + '<br />' +
- 'Device Platform: ' + device.platform + '<br />' +
- 'Device UUID: ' + device.uuid + '<br />' +
- 'Device Model: ' + device.model + '<br />' +
- 'Device Version: ' + device.version + '<br />';
- }
- </script>
- </head>
- <body>
- <pid="deviceProperties">Loading device properties...</p>
- </body>
- </html>
Permissions Required:
- <Capabilities>
- <CapabilityName="ID_CAP_WEBBROWSERCOMPONENT" />
- <CapabilityName="ID_CAP_IDENTITY_DEVICE" />
- <CapabilityName="ID_CAP_IDENTITY_USER" />
- </Capabilities>
You can download the sample which accesses the device details using Cordova and prints it on the screen. You can find detailed API Documentation of PhoneGap here.
I will be blogging on using IgniteUI controls with Cordova in my next blog. And in the later blogs we will also learn about PhoneGap build service.
Cheers,
Abhishek
@narainabhishek