/PhoneGap-Plugin-Own-Phone-Number-iPhone

iPhone PhoneGap plugin to get the phone's number

Primary LanguageObjective-C

iPhone PhoneGap plugin to retrieve own phone number

This project demonstrates how to build and use a simple PhoneGap plugin for iPhone.
The plugin simply returns the phone’s number as a string.

Start by creating a PhoneGap project (we assume version >= 1.0.0)

To implement your own plugin, follow these 4 steps:

  1. Create the plugin – add an Objective-C class to the Plugins folder (see MyPhoneNumberPlugin.h and MyPhoneNumberPlugin.m)
    1. The class extends the PhoneGap PGPlugin class
    2. The function you need to implement has this signature - (void) getMyPhoneNumber:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
    3. The arguments array will contain the names of the 2 success and error functions
  2. Add to plugins list – add a value to the Plugins dictionary that includes the name of your class (see PhoneGap.plist)
  3. Create a JavaScript interface – implement a JavaScript interface to the plugin (see myphonenumber.js)
    1. Notice that it expects an oSuccess and onError callback functions
    2. No need for it to be too elaborate – contain your actual logic in the Objective-C code, and any data preparation to the calling function
    3. There’s more than one way to cll the PhoneGap.exec function, apparently. I ended up with this format after a lot of trial and error (I left earlier versions in the file, commented out)
  4. Call your plugin – (see index.html)
    1. include the js file in the HTML head section
    2. I find it easier to create a single function that contains both oSuccess and onError callback functions, and the actual call

And that’s it. Again, if you keep the logic in the actual plugin Objective-C code, you can more or less reuse this framework as is for future plugins.