In the following mini-tutorial we'll look at how easy it is to add, find, list and remove plugins for Cordova PhoneGap projects using the command line.
We are going to assume that you have the following software installed on your system:
It's also assumed that you have some familiarity with using the command line.
If you can answer yes to all of the above let's get started!
Cordova PhoneGap 3 brought with it not only the command line but also a new way of adding plugins to your projects.
A plugin is a piece of code designed to interface with native device functionality such as vibration, taking photos and connecting to a network for example.
By default all plugins, even core plugins, have to be manually added to a Cordova project by the developer - but this is not as difficult as it initially sounds.
Using the command line navigate to your project directory and install plugins with the following command:
cordova plugin add name_of_plugin_here
Relatively painless right?
As Cordova plugins are able to be added to a project from online repositories using the command line it would make sense that we should be able to search these repositories right?
Piece of cake.
If, for example, we wanted to find all repository plugins for adding purchase functionality to our App we would, in the root of our project, use the search command like so:
cordova plugin search purchase
Which, at the present time of writing, would return the following results:
That's a pretty extensive set of results!
For further repositories connected to the Cordova PhoneGap ecosystem you can visit the following websites:
- Cordova Plugins - Official Cordova plugins registry
- ngCordova - A repository of AngularJS plugins built on top of the Cordova API
- Plugreg - A plugin registry for all Cordova/PhoneGap plugins
To find which plugins are currently installed in your project simply type out the following command:
cordova plugin list // Or the shorthand version cordova plugin ls
Which will output a full list of installed plugins like so:
cordova-plugin-battery-status 1.1.0 "Battery" cordova-plugin-console 1.0.1 "Console" cordova-plugin-device 1.0.1 "Device" cordova-plugin-device-motion 1.1.1 "Device Motion" cordova-plugin-device-orientation 1.0.1 "Device Orientation" cordova-plugin-dialogs 1.1.1 "Notification" cordova-plugin-file 3.0.0 "File" cordova-plugin-file-transfer 1.3.0 "File Transfer" cordova-plugin-geolocation 1.0.1 "Geolocation" cordova-plugin-globalization 1.0.1 "Globalization" cordova-plugin-media 1.0.1 "Media" cordova-plugin-media-capture 1.0.1 "Capture" cordova-plugin-network-information 1.0.1 "Network Information" cordova-plugin-splashscreen 2.1.0 "Splashscreen" cordova-plugin-whitelist 1.0.0 "Whitelist" ionic-plugin-keyboard 1.0.7 "Keyboard"
To remove a plugin from your project simply refer to it by the same identifier shown in the output from the list command. If, for example, we wanted to remove the File Transfer plugin we would issue the following command:
cordova plugin remove cordova-plugin-file-transfer // Or the shorthand version cordova plugin rm cordova-plugin-file-transfer
And, depending on what platforms we have installed for our Cordova PhoneGap project, we would see output akin to the following:
Uninstalling cordova-plugin-file-transfer from android Uninstalling cordova-plugin-file-transfer from ios Removing "cordova-plugin-file-transfer"
Wrapping it up
Thanks to the Cordova application framework and a host of contributions from incredible developer talent all over the world we can add functionality to our projects quickly and easily through a plugin architecture that requires nothing more taxing than a few keystrokes from the command line.