Things to Look at When You Are Using a PhoneGap Plugin

by Buddy Lindsey on December 5, 2011

PhoneGap Logo

PhoneGap is really cool since you can use web development technologies to do native mobile app development. Unfortunately it doesn’t support everything the phone does via its JavaScript API. For that you need to use plugins which execute native code, and is called from JavaScript.

Makeup Of a Plugin

PhoneGap plugins consist of 2 things.

  1. JavaScript File
  2. Native Code File

If you download a plugin from somewhere, and it doesn’t have at least those two things then you are missing something. It is a fairly simple thing to do you write your native code and then write a JavasSript interface for it which executes the native code.

How Do You Execute Native Code?

What you will need to do is create your class and execute it from JavaScript with a simple command:

PhoneGap.exec("");

Inside the exec command you will call the class from your native code, and specify a method. The PhoneGap framework will execute it and return back the information it needs to for you to do with it what you need.

What Does the Native Code Need?

What you really only need is a class that inherits from PGPlugin and has a method for you to call. Inheriting from PGPlugin is important because it can cause a lot of headaches if it isn’t, and I mean a lot.

Tell PhoneGap the Plugin Exists

At least on iOS you need to let your app know the plugin exists, explicitly. Simply edit the plist file with a simple key value pair. The key being some name for JavaScript to call and the value being the class name of the native code implementation.

Why Is It Important to Know at Least These Things

Because if you use PhoneGap you are going to need a plugin at some point, and sometimes you will want to use code which has no documentation or, very little. So knowing at least the basics of what all plugins need you can figure out problems based on error messages. Also it helps you think beyond what PhoneGap does and gives you the opportunity to extend where you need to, especially since plugins aren’t usually that complex to write.

I am having a lot of phone using PhoneGap with jQuery Mobile. It helps me create stuff very quickly, and even do some cool stuff I am not sure I would have figured out how to do after only a couple hours of playing. The biggest benefit is you can quite easily extend PhoneGap with its simple plugin system. Which makes porting to multiple platforms much nicer. Give it a shot and enjoy.

Related Posts:

Was this Helpful?

If you found this article useful you might find others useful as well. Please browse the archives and subscribe to the RSS Feed to stay up-to-date.

Leave a Comment

Previous post:

Next post: