Working with Plugin APIs in a MyEclipse PhoneGap Project
A lot of a functionality can be added using PhoneGap plugin APIs in MyEclipse 2015. This tutorial covers the limited APIs that PhoneGap ships with, though many of the steps can be used for third party APIs as well.
In this tutorial, you will learn how to:
- Add / install a PhoneGap plugin
- Locate plugin documentation
- Add a visual element to the UI
- Test a project
Note: Mobile development was removed in MyEclipse 2016. This tutorial only applies to MyEclipse 2015.
1. Add a Plugin to an Existing Project
- If you don’t already have a PhoneGap project, create a new project.
- Right-click the project, and select PhoneGap>Add / Install Plugin.
- Search for and select the plugin to add, and then click Finish. For the purpose of this tutorial, select org.apache.cordova.inappbrowser.
2. Locate Plugin Documentation to Find Sample Code
As with all default PhoneGap plugins, you can find the documentation in the index.md file located in the plugins/[plugin name]/doc folder.
*Third party plugin documentation could be located in a different directory, or not included. Please consult the developer’s plugin page for more information.
Inside the documentation there is license information, Supported Platforms, Methods, Objects and Examples. Using the supplied examples is a great way to learn how to implement the plugins.
Copy the example code from the index.md file to use in the project.
3. Add Code for the inAppBrowser Plugin
- Expand the project’s js folder, and double-click index.js to open in the editor.
- Create a custom function called openSite.
- Inside the function place the sample code copied from the documentation.
- Save the index.js file.
function openSite() { var ref = window.open('http://apache.org', '_blank', 'location=yes'); ref.addEventListener('loadstart', function(event) { alert(event.url); }); }
This function will be called from a button in the UI.
4. Add a Button to the UI
- Double-click the index.html file to open it in the editor.
- Add a blank line in the file for the button to be placed.
- Drag a button from the palette to the blank line to open the tag wizard.
- Type Open Site into the Label field, and click Finish
- In the source editor, add an on click property to the Button tag to call the openSite function.
The final button code should look similar to this:
<a href="" onclick="openSite();" id="button-1" data-role="button">Open Site</a>
5. Test the project
- Right-click the project, and select PhoneGap>Preview App in Mobile Web Simulator.
- Select a platform.The Mobile Web Simulator opens with your project running.
- Click the Open Site button. An alert dialog appears containing the site URL as well as the site opening in the app.
6. Other Plugin APIs
The traditional set of core Cordova plugins are as follows:
Monitor the status of the device’s battery.
search for – org.apache.cordova.battery-statusCapture a photo using the device’s camera.
search for – org.apache.cordova.cameraWork with the devices contact database.
search for – org.apache.cordova.contactsGather device specific information.
search for – org.apache.cordova.deviceTap into the device’s motion sensor.
search for – org.apache.cordova.device-motionObtain the direction that the device is pointing.
search for – org.apache.cordova.device-orientationVisual device notifications.
search for – org.apache.cordova.dialogsHook into native file system through JavaScript.
search for – org.apache.cordova.fileHook into native file system through JavaScript.
search for – org.apache.cordova.file-transferMake your application location aware.
search for – org.apache.cordova.geolocationEnable representation of objects specific to a locale.
search for – org.apache.cordova.globalizationLaunch URLs in another in-app browser instance.
search for – org.apache.cordova.inappbrowserRecord and play back audio files.
search for – org.apache.cordova.mediaCapture media files using device’s media capture applications.
search for – org.apache.cordova.media-captureNetwork Information (Connection)
Quickly check the network state, and cellular network information.
search for – org.apache.cordova.network-informationShow and hide the applications splash screen.
search for – org.apache.cordova.splashscreenAn API to vibrate the device.
search for – org.apache.cordova.vibration