How to Download and Use Cordova
Introduction
What is Cordova?
Cordova is an open source project that enables you to build mobile apps using web technologies such as HTML, CSS, and JavaScript. It works by wrapping your web app in a native container that can access the device functions of various platforms. These functions are exposed via a unified JavaScript API, allowing you to easily write one set of code that can run on Android, iOS, Windows, Electron, and more.
Why use Cordova?
Some of the benefits and use cases of Cordova are:
- It allows you to reuse your existing web skills and code base.
- It saves you time and money by reducing the need for separate development teams for each platform.
- It gives you access to native device features such as camera, geolocation, file system, etc.
- It supports offline scenarios and progressive web apps.
- It has a large community and ecosystem of plugins and tools.
What are the prerequisites?
To use Cordova, you need to have:
- A basic knowledge of HTML, CSS, and JavaScript.
- A computer with Windows, Mac OS, or Linux operating system.
- A text editor or an IDE of your choice.
- A web browser for testing your app.
- An Android or iOS device or emulator for running your app.
- A Node.js and npm installation for managing Cordova and its dependencies.
Installing Cordova
How to install Node.js and npm?
Node.js is a JavaScript runtime environment that allows you to run JavaScript code outside of a browser. npm is a package manager that comes with Node.js and lets you install and manage modules and dependencies for your projects. To install Node.js and npm, follow these steps:
- Go to the official Node.js website and download the installer for your operating system.
- Run the installer and follow the instructions to complete the installation.
- Open a terminal or a command prompt and type
node -v
andnpm -v
to verify that Node.js and npm are installed correctly. You should see the version numbers of both tools.
How to install Cordova CLI?
Cordova CLI (command-line interface) is a tool that allows you to create, build, run, and manage Cordova projects from the terminal or the command prompt. To install Cordova CLI, follow these steps:
- Open a terminal or a command prompt and type
npm install -g cordova
to install Cordova CLI globally on your system. This may take a few minutes depending on your internet speed. - Type
cordova -v
to verify that Cordova CLI is installed correctly. You should see the version number of the tool.
Creating a project
How to create a blank Cordova project?
A Cordova project is a directory that contains all the files and folders of your app, such as HTML, CSS, JavaScript, images, icons, splash screens, plugins, platforms, etc. To create a blank Cordova project, follow these steps:
- Navigate to the folder where you want to create your project using the terminal or the command prompt.
- Type
cordova create hello com.example.hello HelloWorld
to generate a project with the namehello
, the IDcom.example.hello
, and the display nameHelloWorld
. You can change these values according to your preference. - Type
cd hello
to enter the project directory. - Type
ls
ordir
to see the files and folders of your project. You should see something like this:. ├── config.xml ├── hooks ├── node_modules ├── package.json ├── platforms ├── plugins └── www ├── css │ └── index.css ├── img │ └── logo.png ├── index.html └── js ├── index.js └── cordova.js
The most important files and folders are:
config.xml
: This is the configuration file of your app, where you can specify information such as app name, ID, version, description, author, icons, splash screens, preferences, plugins, etc.hooks
: This is a folder where you can place scripts that will be executed at different stages of your app lifecycle, such as before or after building, running, or preparing your app.node_modules
: This is a folder where npm installs the modules and dependencies of your project.package.json
: This is the package file of your project, where you can specify information such as name, version, description, author, dependencies, scripts, etc.platforms
: This is a folder where Cordova adds the platform-specific files and folders of your app, such as Android or iOS.plugins
: This is a folder where Cordova adds the plugins that you install for your app.www
: This is a folder where you place your web app files, such as HTML, CSS, JavaScript, images, etc. This is where you write your app logic and UI.
You can edit the files in the
www
folder to customize your app. For example, you can open theindex.html
file and change the title, the logo, the text, or the style of your app. You can also add more HTML, CSS, or JavaScript files to your app as you wish.
How to add platforms to your project?
A platform is a target device or operating system where you want to run your app, such as Android or iOS. To add platforms to your project, follow these steps:
- Make sure you have the platform-specific requirements and tools installed on your system. For example, for Android, you need to have Java Development Kit (JDK), Android Studio, Android SDK, and Gradle. For iOS, you need to have Xcode, iOS SDK, and a Mac computer.
- Type
cordova platform add android
to add the Android platform to your project. You should see a message that saysAdding android project...
and some output that shows the progress of the installation. - Type
cordova platform add ios
to add the iOS platform to your project. You should see a similar message and output as before. - Type
cordova platform ls
to list the platforms that you have added to your project. You should see something like this:Installed platforms: android 9.0.0 ios 6.1.0 Available platforms: browser ^6.0.0 electron ^1.0.0 osx ^6.0.0 windows ^7.0.0
You can also add other platforms such as browser, electron, osx, or windows if you want.
How to run your app on different platforms?
To run your app on different platforms, follow these steps:
- Type
cordova run android
to launch your app on an Android emulator or a connected device. You should see a message that saysBUILD SUCCESSFUL
and some output that shows the progress of the deployment. - Type
cordova run ios
to launch your app on an iOS simulator or a connected device. You should see a similar message and output as before. - You can also use the
--emulator
or--device
flags to specify whether you want to run your app on an emulator or a device. For example,cordova run android --emulator
orcordova run ios --device
. - You can also use the
--target
flag to specify which emulator or device you want to use. For example,cordova run android --target="Pixel_3a_API_30_x86"
orcordova run ios --target="iPhone-12"
. - You should see your app running on the chosen platform with a default interface that looks something like this:
You can interact with your app using the emulator or device controls.
Adding plugins
What are Cordova plugins?
Cordova plugins are modules that enable native device features for your app, such as camera, geolocation, file system, etc. They provide a JavaScript API that you can use in your web app to access these features. Cordova plugins are written in native code for each platform, such as Java for Android or Objective-C for iOS.
How to find and install plugins?
To find and install plugins for your app, follow these steps:
- Browse the official Cordova plugin registry, where you can search and view the details of hundreds of plugins available for Cordova.
- Select a plugin that you want to use in your app and copy its name or ID.
- Type
cordova plugin add plugin-name-or-id
to install the plugin in your project. For example,cordova plugin add cordova-plugin-camera
. - You should see a message that says
Installing plugin-name-or-id for platform-name...
and some output that shows the progress of the installation. - Type
cordova plugin ls
to list the plugins that you have installed in your project. You should see something like this:cordova-plugin-camera 5.0.1 "Camera" cordova-plugin-whitelist 1.3.4 "Whitelist"
You can also install plugins from GitHub repositories or local directories by using their URLs or paths instead of their names or IDs. For example,
cordova plugin add https://github.com/apache/cordova-plugin-camera.git
orcordova plugin add ../my-plugin
.
How to use plugins in your app?
To use plugins in your app, follow these steps:
- Read the documentation of the plugin that you want to use and learn how to use its JavaScript API in your web app. For example, for the camera plugin, you can read the official documentation and see how to use the
navigator.camera
object and its methods and properties. - Edit the files in the
www
folder of your project and add the code that calls the plugin API. For example, you can open theindex.js
file and add the following code to take a picture using the camera plugin:// Wait for device API libraries to load document.addEventListener("deviceready", onDeviceReady, false); // Device APIs are available function onDeviceReady() { // Take a picture using the camera navigator.camera.getPicture(onSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI }); } // Called when a photo is successfully retrieved function onSuccess(imageURI) { // Show the image on the screen var image = document.getElementById('myImage'); image.src = imageURI; } // Called if something bad happens function onFail(message) { // Show an error message alert('Failed because: ' + message); }
You can also edit the
index.html
file and add an element to display the image:<img id="myImage" src="" alt="My photo">
- Save your changes and run your app on the desired platform using the
cordova run
command. For example,cordova run android
. - You should see your app running on the chosen platform with a button that says
Capture Photo
. When you tap on it, it will open the camera app and let you take a picture. When you accept the picture, it will show it on the screen.
Conclusion
In this tutorial, you have learned how to download, install, and use Cordova to create a cross-platform mobile app using web technologies. You have also learned how to add platforms and plugins to your project and how to run your app on different devices. You have created a simple app that uses the camera plugin to take and display a picture.
Cordova is a powerful and flexible platform that allows you to create amazing apps with native device features using HTML, CSS, and JavaScript. You can explore more features and possibilities of Cordova by reading the official documentation, browsing the official plugin registry, or joining the official community forum. You can also check out some of the showcase apps that are built with Cordova.
FAQs
What is the difference between Cordova and PhoneGap?
Cordova and PhoneGap are both platforms that enable you to build mobile apps using web technologies. However, they have different origins and relationships. Cordova was originally created by a company called Nitobi, which was acquired by Adobe in 2011. Adobe then donated Cordova to the Apache Software Foundation, where it became an open source project. PhoneGap is Adobe’s distribution of Cordova, which includes some additional features and services such as PhoneGap Build, PhoneGap Desktop, PhoneGap CLI, etc. PhoneGap is compatible with Cordova plugins and tools, but it may have different versions and release cycles.
What is the difference between Cordova and React Native?
Cordova and React Native are both platforms that enable you to build cross-platform mobile apps using JavaScript. However, they have different approaches and trade-offs. Cordova works by wrapping your web app in a native container that can access the device features via a JavaScript API. This means that you can reuse your existing web skills and code base, but it also means that your app may not have the best performance or user experience compared to native apps. React Native works by compiling your JavaScript code into native code that can render native UI components and interact with the device features via a native bridge. This means that you can have a better performance and user experience compared to web apps, but it also means that you need to learn a new framework and write some platform-specific code.
How can I debug my Cordova app?
There are several tools and methods that you can use to debug your Cordova app on different platforms. Some of the most common ones are:
- Using the browser developer tools: You can use the Chrome DevTools or the Safari Web Inspector to debug your app on Android or iOS devices or emulators. You can inspect the HTML, CSS, and JavaScript code, view the console logs, set breakpoints, watch variables, etc.
- Using the Cordova CLI: You can use the
cordova serve
command to run a local web server that hosts your app files. You can then access your app from any browser on your network and debug it using the browser developer tools. - Using the Visual Studio Code: You can use the Visual Studio Code editor and its extensions such as Cordova Tools or Debugger for Chrome to debug your app on Android or iOS devices or emulators. You can edit, run, and debug your app from within the editor, using its features such as IntelliSense, code completion, syntax highlighting, etc.
How can I customize my app icons and splash screens?
To customize your app icons and splash screens, you have two options:
- Using the
cordova-res
tool: This is a command-line tool that automatically generates and copies icons and splash screens for all platforms based on a single source image. You can install it usingnpm install -g cordova-res
and use it by typingcordova-res
in your project directory. You need to have anicon.png
file and asplash.png
file in yourresources
folder. - Manually editing the
config.xml
file: This is a more manual and flexible way of customizing your app icons and splash screens. You can edit the<icon>
and<splash>
tags in yourconfig.xml
file and specify the paths and sizes of your images for each platform. You need to have different images for different resolutions and densities in yourres
folder.
How can I publish my Cordova app to the app stores?
To publish your Cordova app to the Google Play Store or the Apple App Store, you need to follow these steps:
- Build your app for the desired platform using the
cordova build
command. For example,cordova build android --release
orcordova build ios --release
. This will generate an APK file for Android or an Xcode project for iOS in yourplatforms
folder. - Sign your app with a valid certificate and key. For Android, you need to use the
jarsigner
tool and thezipalign
tool to sign and align your APK file. For iOS, you need to use the Xcode to sign your app with a provisioning profile and a distribution certificate. - Publish your app to the app store using the appropriate tools and methods. For Android, you need to use the Google Play Console to upload your APK file and fill in the required information and metadata. For iOS, you need to use the Xcode or the Application Loader to upload your app to iTunes Connect and fill in the required information and metadata.
- Wait for the approval process and monitor the performance and feedback of your app.
bc1a9a207d