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
- # Ifdef PHONEGAP_FRAMEWORK
- # Import
- # Else
- # Import "PGPlugin. h"
- # Endif
- @ Interface MyPGPlugin: PGPlugin {
- }
- -(Void) addStr :( NSMutableArray *) arguments withDict :( NSDictionary *) options;
- @ End
|
MyPGPlugin. m
Copy code
- # Import "MyPGPlugin. h"
- @ Implementation MyPGPlugin
- -(Void) addStr :( NSMutableArray *) arguments withDict :( NSDictionary *) options
- {
- NSLog (@ "% @", arguments );
- NSString * callbackId = [arguments pop];
- NSString * result = [NSString stringWithFormat: @ "% @", [arguments objectAtIndex: 0], [arguments objectAtIndex: 1];
- PluginResult * pr = [PluginResult resultWithStatus: PGCommandStatus_ OK messageAsString: result];
- [Self writeJavascript: [pr toSuccessCallbackString: callbackId];
- }
- @ End
|
3. Create a New js file in the www folder. Here I also name it MyPGPlugin.
Copy code
- /**
- * Constructor
- */
- Function MyPGPlugin (){
- };
- /**
- * String Addition
- *
- * @ Param str1, str2
- */
- MyPGPlugin. prototype. addStr = function (successCallback, failureCallback, str1, str2 ){
- PhoneGap.exe c (successCallback, failureCallback, "MyPGPlugin", "addStr", [str1, str2]);
- };
- /**
- * Load the MyPlugin object
- */
- PhoneGap. addConstructor (function (){
- If (! Window. plugins)
- {
- Window. plugins = {};
- }
- Window. plugins. myPGPlugin = new MyPGPlugin ();
- });
|
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
- <Script type = "text/javascript" charset = "UTF-8" src = "MyPGPlugin. js"> </script>
|
Defines js functions. After the onDeviceReady function is defined
Copy code
- Function addStr (){
- Var str1 = "str1 ";
- Var str2 = "str2 ";
- Window. plugins. myPGPlugin. addStr (successCallback, failureCallback, str1, str2 );
- }
- // Callback method when the call is successful
- Function successCallback (data ){
- Alert ("result:" + data );
- }
- // Method returned when a failure occurs
- Function failureCallback (data ){
- Alert ("error:" + data );
- }
|
Add a button and test it.
Copy code
- 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!