In this article

Getting Started With Visual Studio Tools For Apache Cordova

04/13/2016

30 minutes to read

Contributors

In this article

Use Visual Studio Tools for Apache Cordova to build mobile apps using web technologies (HTML, CSS, JavaScript) that run on Android, iOS, and Windows devices. Since Cordova apps are built using web application code, you'll be able to share the majority of your code across device platforms; learn more.

In this guide, we'll show how to build, test and debug a Cordova application using the Visual Studio Tools for Apache Cordova. Along the way, you'll build a simple mobile weather app that looks like this:

The application displays current weather conditions based on the current device location plus enables users to search for weather conditions using a US postal (zip) code. You can find the application's source code here.

Create An Apache Cordova Project

Open Visual Studio and create a new project by opening the File menu, select New then Project; you can also use the key combination: Ctrl+Shift+N.

In the New Project dialog, open the list of installed templates, and expand the JavaScript option. In the list of templates that appears there, select Blank App (Apache Cordova) as shown in the figure:

Note

If you prefer to use TypeScript instead of JavaScript for this app, expand Other Languages in the Installed list, select TypeScript, and then select Blank App (ApacheCordova) as shown in the following figure:

Name the project WeatherApp, specify a target folder, and then click the OK button to create the project.

During the project creation process, Visual Studio creates a Solution for the project then uses the Cordova command line interface (CLI) and other tools to create the Cordova application project folder structure, add platforms to the project (Android, iOS, and Windows), and populates the project's web application with default content from the template.

At this point, you've created a complete Apache Cordova project and you're ready to start coding.

A Quick Cordova Project Tour

Before we start coding the application, lets take a look at the project structure created by Visual Studio Tools for Apache Cordova.

The following figure shows the project folder structure and files for the newly created Apache Cordova project. The sections that follow will describe the folders and files included in the project.

www Folder

The project's www folder contains the web application content files for the Cordova application, so most of your development will occur inside this folder. Here you'll find the web application content that is packaged into a native mobile application by the Cordova SDK. The HTML, JavaScript, and CSS files in this folder will render in the Cordova app the same way they will in a mobile browser, so be sure to optimize the content for the smaller and more limited smartphones and tablets. The folder's index.html file is the main entry point for the web application, it's loaded by default when the Cordova application launches. The folder contains several subfolders that follow a typical web application folder structure:

css - Stores the web application's Cascading Style Sheet (CSS) files. The index.css shown in the folder is the standard .css file included with the default Cordova application template.

images - Stores any image files used by the web application. The cordova.png file shown in the folder is the Cordova logo that displays in the center of the application screen in the default Cordova application template.

scripts - Stores JavaScript files used by the web application. The project's index.js file contains the bootstrap code that initializes the Cordova application; it registers callback functions for the Cordova deviceReady, onPause and onResume events. The platformOverrides.js file works in conjunction with similar files located in the merges folder to enable developers to deliver different code per target platform. See merges Folder for additional information.

Cordova developers do most of their work in these folders, creating and updating web application content designed for mobile devices. The application content contained herein is packaged into a native mobile application by Visual Studio for deployment to mobile devices using the Cordova and device platform SDKs.

merges Folder

Apache Cordova uses the merges folder to provide developers with the ability to deliver different content based on the target mobile platform. As you can see from the figure, there's a subfolder for each target mobile platform (Android, iOS and Windows). Any content in these folders will be copied to the native application project during the Cordova pre-build process (prepare), adding to or replacing content in the web application folder as needed.

For example, any content in the merges\android folder is copied to the Android project's web application folder after the base web application is copied. Content in the merges\ios folder is copied into the iOS project's web application folder. Finally, content in the merges\windows folder is copied to the Windows project's web application folder after the base web application is copied.

To help deliver a consistent web application experience in Cordova apps, the default project created by Visual Studio Tools for Apache Cordova includes platformOverrides.js files for Android (in merges\android\scripts\platformOverrides.js) and Windows (in merges\windows\scripts\platformOverrides.js) along with a platform-specific JavaScript library. On Android, it adds a polyfill for bind() on older Android devices, and on Windows it adds Microsoft's safeHTML polyfill library. The merges/ios folder doesn't include a platformOverrides.js file, so there's no platform-specific override, iOS applications get the generic version of the file located in www/scripts/platformOverriders.js.

build.json - Configuration file for the Android and iOS build process. Do not delete this file, as the Cordova SDK uses it to build signed mobile app packages; learn more about it here.

config.xml - Cordova project configuration file, it contains settings that define options for the Cordova native mobile application including application name, plugin configuration, security settings, and more. Do not delete this file, the Cordova SDK will not be able to build and deploy applications without it. Visual Studio Tools for Apache Cordova includes a special editor for this file, abstracting away the file's XML into a simple interface to the file's settings.

package.json - Configuration file for the Node Package Manager (npm). This file isn't used by Cordova projects today, but will eventually become more important when it replaces the config.xml as a Cordova project's primary configuration file in a future version of Apache Cordova.

Run The App Using Cordova Simulate

The default Cordova project includes a simple application user interface and logic, enabling you to immediately run a simple sample application to validate that everything is working. In this section, we'll launch the application in order to highlight some of the capabilities of Visual Studio Tools for Apache Cordova; later, we'll add our own web content to complete the project.

Cordova Simulate requires the Google Chrome browser; make sure you have the browser installed before running the application.

Visual Studio will launch Cordova Simulate, a browser-based Cordova application simulator. Cordova Simulate displays the Cordova application's content in a browser window sized for the target mobile device. Here you can interact with the application, using your mouse to simulate finger taps and touches. What you see in the figure is the default Cordova app, included with every Cordova project. It doesn't expose any application functionality you can interact with - it simply lets you know the app initialized correctly.

Visual Studio exposes a set of panels you can use to manipulate options within the simulator. Switch back to Visual Studio and you'll see that it opened the DOM Explorer for you. Use the DOM explorer like you would other web development tools, navigating through the DOM hierarchy looking for specific elements, changing attributes, and poking and prodding at the app as it runs.

Use the Plugin Controls window to simulate settings in the Cordova plugins added to the project. By default Visual Studio shows settings for the Geolocation and Device plugins, plus provides an interface you can use to fire certain events as the application runs. As you add additional Cordova plugins to your project, additional panes will appear in the Plugin Controls page for you to use.

Stop the Cordova Simulate debug session by clicking the Stop button in the Standard Toolbar.

You can also run your app on emulators and simulators for Android, iOS, and Windows devices. First, choose a target mobile device platform from the platform list on the Standard toolbar.

Note

Deploying to iOS simulators or devices requires access to a Apple Mac configured with Xcode and the Visual Studio remotebuild module. Refer to Visual Studio Tools for Apache Cordova: iOS Guide for guidance on how to configure your development environment for iOS.

Next, click the drop-down arrow on the deployment target in the Standard toolbar to see a list of available targets. Choose a deployment target from the list and Visual Studio will build the app and deploy it to the selected device, emulator or simulator.

If a particular emulator or simulator family is missing from the list of available options, click the Install item in the list to add them to your system's configuration. Visual Studio Tools for Apache Cordova doesn't install all available targets by default, but provides this simple mechanism for adding targets to an existing system.

Configure The Cordova Native Application

The configuration settings for the Cordova native application are maintained in the project's config.xml file. Rather than force you to manually edit the file's XML content, Visual Studio Tools for Apache Cordova includes a custom editor that abstracts away the complexities of XML and enables you to set the file's properties using standard Windows input fields.

To edit the project's config.xml, double-click on the file in the Visual Studio Solution Explorer; Visual Studio will open the custom editor for the file shown in the following figure:

The editor exposes the following options:

Toolset - Used to configure the target Cordova platform and tools version for the application; you can learn more about this feature here.

Common - Used to define Cordova application settings that apply to all target platforms (such as display name, start page, application version, and more).

Plugins - Used to add and remove Cordova plugins to/from the project.

Windows - Used to define settings for the Windows Store version of the application.

Android - Used to define settings for the Android version of the application (such as minimum and maximum API versions and other properties for the application).

iOS - Used to define settings for the iOS version of the application (such as target device and target OS version, and other properties for the application).

If you want, give the app a more appropriate Display Name, modify the application version, and adjust other aspects of the app's behavior using the editor.

Note

To edit the config.xml file directly, without using the special editor Visual Studio provides, you can right-click on the file in Solution Explorer and select View code or highlight the file in Solution Explorer then press the F7 key.

Creating The Sample Application

Now that you have a functional Cordova application project, lets remove the boilerplate web application content included in the default Cordova application project and replace it with the weather app code.

Add jQuery Packages to the Project

The application uses the jQuery and jQuery Mobile libraries to help craft the app's UI and simplify the code. jQuery is a JavaScript library that makes certain tasks easier such as changing the properties of a control or handling its events. jQuery Mobile is a touch-optimized HTML5 UI framework, built on jQuery, designed to make responsive web sites and apps that are accessible on all smartphone, tablet and desktop devices. Together, they give us a quick way to style the application without having to do custom CSS work.

Let's add the JQuery and JQuery Mobile NuGet packages to the project.

Note

Before starting the following steps, point your browser of choice to the jQuery Mobile download site to note which version of the jQuery library is compatible with the current shipping release of jQuery Mobile. At the time of this writing, jQuery Mobile version 1.4.5 is compatible with jQuery version 1.8 through 1.11. In the steps that follow, be sure you install compatible versions of both frameworks. The NuGet Package Manager should deploy compatible versions of dependent libraries, but it's best to make sure first.

When you click the Install button, the NuGet Package Manager will display a confirmation dialog highlighting the components being installed. Notice that it's installing jQuery version 1.8 rather than the latest version for compatibility reasons.

NuGet adds files to the Visual Studio project's Scripts folder, but they really belong in the Cordova project's www/scripts folder. Drag all of the .js files from the Scripts folder to the www/scripts folder as shown in the figure.

NuGet also installs the frameworks' CSS files to the Visual Studio project's Content folder, but they really belong in the Cordova project's www\css folder. Drag the images folder plus all of the .css files from the project's Content folder to the www\css folder as shown in the figure.

Add this script reference to the <body> tag of your project's index.html file. Place the content at the bottom of the body section, where the other JavaScript references are located:

Add the following style reference to the <head> tag of your project's index.html file. Place the content above the existing index.css reference:

<link rel="stylesheet" href="css/jquery.mobile-x.x.x.min.css" />

As you did in the previous step, replace the x.x.x. in the statement with the jQuery Mobile version you recently installed into your project. For the example versions described earlier, the statement look like this:

<link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css" />

Save your changes to the project's index.html file by pressing Ctrl-S or by opening the File menu and selecting Save.

Building The Application's UI

The Cordova project's index.html is the default page for the application; the Cordova native application loads the page as the app's main screen when a Cordova application launches. To create the application's UI, we'll add content to this file.

Note

You can change the default page by populating the Start Page property in the project's config.xml file using the Configuration Editor.

Open the project's www\css\index.css file and replace its content with the following:

This code creates the application's main UI. It adds an input form to the page with a single search input field and button. It also adds a jQuery Mobile ListView to render the weather forecast for the selected location.

Add The Find Weather Button Click Listener

Next we'll add code that reacts to the user tapping the Find Weather button.

Open the project's www\scripts\index.js file, and remove the following code from the file's onDeviceReady function.

This code updates the main page to indicate that the Cordova container finished initializing; we've already replaced the HTML markup that this code refers to, so it's no longer needed.

Next, add the following line of code to www\scripts\index.js file's onDeviceReady function.

$('#get-weather-btn').click(getWeatherWithZipCode);

This code adds a click listener to the Find Weather button, executing the getWeatherWithZipCode function every time the button is tapped. The getWeatherWithZipCode function doesn't exist yet, but we'll add it soon.

A Quick Look At index.js

This is a good time to take a quick look at the project's index.js file. This file loads when the user runs the app because the index.html page contains this reference to it:

<script src="scripts/index.js"></script>

You'll notice a few things about the index.js file. First, all the code in that file is enclosed within an anonymous self-invoking function (or Immediately Invoked Function Expression, or IIFE). This is a fancy way of saying that this function executes automatically when the file loads.

The function adds an event handler for the Cordova deviceready event; the event fires after the Cordova native application container finishes initializing.

Cordova plugins extend the capabilities available to a Cordova application. In general, they expose native application functionality to the web application running in the Cordova container. Cordova publishes plugins for standard native capabilities like Camera, Device Motion (accelerometer), Device Orientation (compass), and so on. Third-party developers also produce Cordova plugins you can use in your Cordova apps. These plugins expose APIs through a JavaScript interface in the application, and Cordova applications cannot make calls to any Cordova plugin APIs until after the native container has initialized.

The onDeviceReady function is used to kick off any application processes that leverage any Cordova APIs, or at least set a flag that indicates to the application that it can use the Cordova APIs. As you'll see later, we'll use this function to populate the current weather conditions for the device's current location on application startup.

Get an OpenWeatherMap API Key

The app we're creating uses the free OpenWeatherMap service to retrieve weather conditions for a location. Before you can use the service, you must setup an account and request an API key for their Current Conditions API. Point your browser to OpenWeatherMap and setup an account. Once you have a valid login, go to the service's API page and subscribe to the Current Weather data service, and then generate an API key.

Note

Make note of the API key as you'll need it later in the app's JavaScript code.

Add Code To Get Current Weather Conditions

Now we'll add the function that responds to the Find Weather button's click event. But first, let's create a JavaScript file to keep that functionality separate.

In Solution Explorer, expand the project's www folder, right-click on the scripts folder, and then choose Add -> New JavaScript file. Name the file weather.js, and then click the Add button.

Visual Studio will open the newly created weather.js file for editing. Paste the following code into the file:

In the OpenWeatherAppKey variable, replace Your_Key_Here with the API key you generated on OpenWeatherMap.

The getWeatherWithZipCode function retrieves the zip code from the input field, and calls the OpenWeatherMap service to get the weather for the specified location. The showWeatherData function updates the page with the current weather conditions retrieved from the service.

If you ran the code now, nothing interesting would happen. We still have to tell the application to use the code in the weather.js file.

Open the project's www\index.html page, and add the following script tag to the <body> of the page (at the bottom of the body section, where the other JavaScript references are located).

<script src="scripts/weather.js"></script>

When you're done, the list of scripts loaded by the index.html should look like the following:

To enable the web app to connect to the OpenWeatherMap services, we need to add the weather service API endpoint (http://api.openweathermap.org) to the page's Content Security Policy (CSP). The CSP is an HTML meta tag located inside the page's <head> section; it's used to define approved content origins browsers are allowed to load in the page. It currently looks like this:

Content Security Policy (CSP) is a computer security standard introduced to prevent cross-site scripting (XSS), clickjacking and other code injection attacks resulting from execution of malicious content in the trusted web page context. You can read more about at content-security-policy.com.

The Cordova Whitelist plugin is added automatically to every Cordova project. It adds an extra layer of security to the Cordova native application by enabling developers to define, at the native application layer, which endpoint domains the application can access. By default, the whitelist is setup so the application can access any endpoint through the inclusion of * in the Domain Access field in the project's config.xml as shown in the following figure:

This approach is fine during development, but before you put an application into production, you should restrict access to only the domains that you know the app needs to access.

To fix this, open the Cordova Configuration Editor then access the Common tab. Click the Add new URI button then populate the input field with http://api.openweathermap.org. Next, click the Remove button next to the * entry. The end result should look like the following:

Run The App In An Android Emulator

Now that the app has the code it needs to retrieve weather conditions, let's run it, but this time use a device emulator.

In the list of Android emulators, choose the one of the Google Emulator options (Phone or Tablet). Visual Studio will build the mobile app, launch the selected emulator, then deploy the app in the emulator.

Note

Depending on your PC configuration some warnings or errors might appear, like firewall alerts or a message that indicating that the emulator must run in elevated mode. If you receive the following error, stop the Android Debug Bridge process (adb.exe) and retry launching the emulator:

In the Android emulator, type a valid United States zip code into the edit box (for example: 44313), and then press the Find Weather button. Current weather data for target zip code will appear on the page.

The app we've built is completely cross platform, it should run anywhere. If you have a little extra time, run this app in a Windows Phone emulator.

Note

You should test your app on as many physical devices (running on as many Operating System versions) as you can. Emulators and Simulators often behave different than a real device on certain circumstances and respond at different speeds. For example, an app may work in the simulator without a problem, but it may fail on a real device, or it can run faster on the device compared with an emulator. More information here.

Find and Fix problems

A big benefit of Visual Studio is its powerful debugger. If you're already familiar with it, you can skip this section. If you're not, let's take a quick tour of some things that you can do.

Set Breakpoints and Conditional Breakpoints

You can set a breakpoint on any line of executable code (including the JavaScript code) within the application, view the state of the Cordova application, monitor variable values, review call stacks, and more. In the following figure, you can see that a breakpoint has been set on the line of code that calls the weather service to retrieve data. Visual Studio highlights the entire scope of the selected line; when running the app from within the IDE (on an emulator, simulator or physical device) Visual Studio will stop execution when it reaches that line and open the debugger to enable you to interact with the code.

You can use the console.log method to log messages and then view those messages in the console. You can also use the console to execute any JavaScript function while the application is running. Learn more about the JavaScript Console. As an example, to generate the output highlighted output in the figure, the following code was added to the beginning of the getWeatherWithZipCode function:

console.log('entering getWeatherWithZipCode');

Use the Document Object Model (DOM) Explorer

Use the DOM Explorer to find and fix layout issues in your pages. Examine the structure of your pages as they render and then tweak them while the app is running. To open the DOM Explorer, choose Debug -> Windows -> DOM Explorer while the app is running. Learn more about the DOM Explorer.

Access Device Capabilities Through Plugins

Most likely you'll want your app to do more than just show web pages on a mobile device. You might want your app to store files, receive push notifications, or capture images by using the device's camera. This is where Cordova plugins becomes handy. Apache Cordova uses plugins to deliver access to native device capabilities that aren’t typically available to web applications.

The Visual Studio Configuration Editor Plugin tab provides an interface that enables you to add and remove plugins from a Cordova application project. The Core tab lists the core Cordova plugins, plugins published and maintained by the Cordova development team. The green checkboxes shown in the figure indicate installed plugins. The Installed tab lists only the plugins that are installed in the application.

Use the Custom page to add plugins that are not one of the Cordova core plugins. Usually these are plugins created by you or a third-party. This document explains how to add a custom plugin to your project.

If you need to use a specific version of a plugin, you can add a plugin by editing the config.xml file directly.

Let's add a plug-in that gives us access to the device's geolocation capabilities; with this in place, we'll adjust the app so it retrieves weather conditions for the device's current location when the application launches.

On the Core tab, scroll down to the Geolocation plugin, select the plugin, then click the Add button to add the plugin to your project.

The Cordova CLI adds plugins to the project's plugins folder, with a separate folder for each plugin. To learn how to use the plugin in your application, open the Readme.md file in the plugin's folder as shown in the following figure:

Let's add some code that consumes this plugin.

In the project's www\scripts\weather.js file add the following code right after the getWeatherWithZipCode function

This code uses the device's geolocation capability to get the latitude and longitude of the device's location. It then uses that information to query the weather service for current conditions for the current location.

Open the project's www\scripts\index.js file, and add the following code to the onDeviceReady function:

getWeatherWithGeoLocation();

This code executes the getWeatherWithGeoLocation function when the deviceready event fires; updating the page with the weather conditions for the current location. The resulting onDeviceReady function should look like this:

Run the app using Cordova Simulate or one of the Android emulators. When the app starts, the page will display Determining your current location ... as it queries the Cordova Geolocation API for the current location. After a short pause, weather conditions for the current location displays on the page.

To adjust the simulated device location in Cordova Simulate, switch to the Visual Studio Plugin Controls window (it opens automatically when you launch Cordova Simulate) and enter latitude and longitude values for the location then reload the app in the Cordova Simulate browser. Cordova Simulate automatically refreshes web content when you change it in Visual Studio, but since the app checks the current location on application start up, you'll need to restart the Cordova Simulate session in Visual Studio to see the location change within the app.

To adjust the Android emulator location, press the More button in the panel to the right of the emulator window (the More button is the one with the three horizontal dots). In the Extended Controls window that appears, specify latitude and longitude values for the location you want to set, then press the Send button to send the location to the emulator.

Tailor The App To A Platform

The code that you used to get the device's location works pretty well across all platforms, but what if it didn't? What if you had to write extra code to get the location of an iOS device? This is where the merges folder becomes useful. We touched on that folder earlier in this article. Now let's try a few basic things.

Tailor The Behavior of Your App

In Solution Explorer, expand the www folder, and then the scripts folder.

Right-click the weather.js file, and choose Copy.

In Solution Explorer, expand the merges folder, then the android folder.

Right-click on the scripts folder, and then choose paste. You'll now have a copy of weather.js in merges\android\scripts folder.

In the weather.js file (located in the merges\android\scripts folder), add the following code to the showWeatherData function.

$('#app-title').text("Android Weather");

Run your app using an Android target in Cordova Simulate. The app will display Android Weather in the title bar because the weather.js file in the merges\android\scripts folder replaced the weather.js file in the www\scripts folder for the Android version of the application.

Making a copy of a file this large to change one line of code is an extreme example, but you can begin to imagine the possibilities, and you can always refactor your code in ways that leverage the merges folder more efficiently.

Tailor The Appearance of Your App

In some cases, you'll want to tweak the layout of your pages so that they appear correctly for one type of device or another.

In Solution Explorer navigate to the project's merges\android folder.

Right-click the android folder, choose Add -> New Folder, and name the folder css.

Right-click the new css folder, and then select Add -> New CSS file.

Name the file index.css, and then click the Add button.

You'll now have a file named index.css in both the merges\android\css and www\css folders.

Replace the contents of the new merges\android\css\index.css file with the following code.

This is a simple example of what you can do using the Cordova project's merges folder. A more effective approach would be to use a separate CSS file for platform-specific styles.

Run your app in Cordova Simulate.

The app header has a blue background because the index.css file in merges\android\css folder replaced the index.css file in the www\css folder for the Android version of your app.

Next steps

Congratulations on building your first cross-platform mobile app using Apache Cordova and Visual Studio Tools for Apache Cordova. Although this article just gets you started, there’s so much more to learn. Here are a few ideas about what you can explore next on your journey to build mobile apps with HTML and JavaScript and Visual Studio.

Additionally, the server or servers that you request data from may have additional security that restricts your app from being able to pull resources into your app:

Cross-origin resource sharing (CORS) policies regulates which resources (e.g. fonts, JavaScript, etc.) from a web page can be requested from another domain outside the domain from which the resource originated. So, often times when your app is pulling data, fonts, images, and scripts from another web service, you need to make it in the form of a CORS request. See more information.

Easy Backend Set Up with Microsoft Azure

Get straight to building by hooking your app up to Azure. Azure offers a Mobile App service that utilizes Easy Tables to get your app connected to a SQL database with only a couple lines of JavaScript. This helpful guide takes you through the process of creating a mobile app backend, and ends with the option of creating a quickstart Cordova app, or adding connections to an existing app.

Explore Available Cordova plugins

Search for available Cordova plugins on npm. The naming convention for these plugins is cordova-plugin-plugin_name or phonegap-plugin-plugin_name.

Tip: If find a plugin that doesn't appear in the Plugins tab of configuration designer, you can still use it. Learn more.