PhoneGap plug-in development example (iOS)

Source: Internet
Author: User

Important Note: If you use both PhoneGap (cordova) and sencha-touch, you must first introduce the phonegap js file, otherwise, the plug-in you developed may not be called!


PhoneGap introduction:
Official PhoneGap website
PhoneGap China
Simple Steps for PhoneGap plug-in development:
1. Create a PhoneGap-based project. If you do not have the PhoneGap SDK, go to the official website to download and install it. Currently, I am using PhoneGap1.3.0.
2. Create a subclass of the PGPlugin class under the Plugins group and name it MyPGPlugin.
MyPGPlugin. h

Copy code
  1. # Ifdef PHONEGAP_FRAMEWORK
  2. # Import
  3. # Else
  4. # Import "PGPlugin. h"
  5. # Endif
  6. @ Interface MyPGPlugin: PGPlugin {
  7. }
  8. -(Void) addStr :( NSMutableArray *) arguments withDict :( NSDictionary *) options;
  9. @ End


MyPGPlugin. m
Copy code
  1. # Import "MyPGPlugin. h"
  2. @ Implementation MyPGPlugin
  3. -(Void) addStr :( NSMutableArray *) arguments withDict :( NSDictionary *) options
  4. {
  5. NSLog (@ "% @", arguments );
  6. NSString * callbackId = [arguments pop];
  7. NSString * result = [NSString stringWithFormat: @ "% @", [arguments objectAtIndex: 0], [arguments objectAtIndex: 1];
  8. PluginResult * pr = [PluginResult resultWithStatus: PGCommandStatus_ OK messageAsString: result];
  9. [Self writeJavascript: [pr toSuccessCallbackString: callbackId];
  10. }
  11. @ End

3. Create a New js file in the www folder. Here I also name it MyPGPlugin.
Copy code
  1. /**
  2. * Constructor
  3. */
  4. Function MyPGPlugin (){
  5. };
  6. /**
  7. * String Addition
  8. *
  9. * @ Param str1, str2
  10. */
  11. MyPGPlugin. prototype. addStr = function (successCallback, failureCallback, str1, str2 ){
  12. PhoneGap.exe c (successCallback, failureCallback, "MyPGPlugin", "addStr", [str1, str2]);
  13. };
  14. /**
  15. * Load the MyPlugin object
  16. */
  17. PhoneGap. addConstructor (function (){
  18. If (! Window. plugins)
  19. {
  20. Window. plugins = {};
  21. }
  22. Window. plugins. myPGPlugin = new MyPGPlugin ();
  23. });

4. Modify PhoneGap. plist, expand Plugins, and add a new column by "+". Add key MyPGPlugin and value MyPGPlugin.
5. Add the following code to index.html:
Import js files
Copy code
  1. <Script type = "text/javascript" charset = "UTF-8" src = "MyPGPlugin. js"> </script>

Defines js functions. After the onDeviceReady function is defined
Copy code
  1. Function addStr (){
  2. Var str1 = "str1 ";
  3. Var str2 = "str2 ";
  4. Window. plugins. myPGPlugin. addStr (successCallback, failureCallback, str1, str2 );
  5. }
  6. // Callback method when the call is successful
  7. Function successCallback (data ){
  8. Alert ("result:" + data );
  9. }
  10. // Method returned when a failure occurs
  11. Function failureCallback (data ){
  12. Alert ("error:" + data );
  13. }

Add a button and test it.
Copy code
  1. MyPGPlugin Test

6. Compile and run the program.

Reference link:
Http://www.adobe.com/devnet/html5/articles/extending-phonegap-with-native-plugins-for-ios.html
Http://prostorys.blog.51cto.com/1522778/843699
Http://wiki.phonegap.com/w/page/36753496/How%20to%20Create%20a%20PhoneGap%20Plugin%20for%20iOS (Wall Mounted)

The following code is provided without points!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.