facebook

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

  1. If you don’t already have a PhoneGap project, create a new project
  2. Right-click the project, and select PhoneGap>Add / Install Plugin.
    add-plugin-menu
    Adding a plugin
  3. Search for and select the plugin to add, and then click Finish. For the purpose of this tutorial, select org.apache.cordova.inappbrowser.
    select inappbrowser
    Select your plugin

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.  

plugin docs
Plugin documentation 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.

copy sample code
Copying sample code

3. Add Code for the inAppBrowser Plugin

  1. Expand the project’s js folder, and double-click index.js to open in the editor.
  2. Create a custom function called openSite.
  3. Inside the function place the sample code copied from the documentation.
  4. 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

  1. Double-click the index.html file to open it in the editor.
  2. Add a blank line in the file for the button to be placed.
  3. Drag a button from the palette to the blank line to open the tag wizard.
  4. Type Open Site into the Label field, and click Finishbutton tag wizardInsert Tag Wizard
  5. 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

  1. Right-click the project, and select PhoneGap>Preview App in Mobile Web Simulator. 
  2. Select a platform.The Mobile Web Simulator opens with your project running. 
  3. Click the Open Site button. An alert dialog appears containing the site URL as well as the site opening in the app.
    app in simulatorApp running in the Web Simulator

6. Other Plugin APIs

The traditional set of core Cordova plugins are as follows:

  • Battery Status

    Monitor the status of the device’s battery.
    search for – org.apache.cordova.battery-status

  • Camera

    Capture a photo using the device’s camera.
    search for – org.apache.cordova.camera

  • Contacts

    Work with the devices contact database.
    search for – org.apache.cordova.contacts

  • Device

    Gather device specific information.
    search for – org.apache.cordova.device

  • Device Motion (Accelerometer)

    Tap into the device’s motion sensor.
    search for – org.apache.cordova.device-motion

  • Device Orientation (Compass)

    Obtain the direction that the device is pointing.
    search for – org.apache.cordova.device-orientation

  • Dialogs

    Visual device notifications.
    search for – org.apache.cordova.dialogs

  • FileSystem

    Hook into native file system through JavaScript.
    search for – org.apache.cordova.file

  • File Transfer

    Hook into native file system through JavaScript.
    search for – org.apache.cordova.file-transfer

  • Geolocation

    Make your application location aware.
    search for – org.apache.cordova.geolocation

  • Globalization

    Enable representation of objects specific to a locale.
    search for – org.apache.cordova.globalization

  • InAppBrowser

    Launch URLs in another in-app browser instance.
    search for – org.apache.cordova.inappbrowser

  • Media

    Record and play back audio files.
    search for – org.apache.cordova.media

  • Media Capture

    Capture media files using device’s media capture applications.
    search for – org.apache.cordova.media-capture

  • Network Information (Connection)

    Quickly check the network state, and cellular network information.
    search for – org.apache.cordova.network-information

  • Splashscreen

    Show and hide the applications splash screen.
    search for – org.apache.cordova.splashscreen

  • Vibration

    An API to vibrate the device.
    search for – org.apache.cordova.vibration