1. Create a phonegap project myplugin
Environment
Xcode4.5.1
Phonegap2.1.0
2. Create an implementation in subclass under the plugin folder
//// MyPlugin.h// MyPGPlugin//// Created by kllmctrl on 12-10-29.////#import <Cordova/CDV.h>@interface MyPlugin : CDVPlugin- (void) add:(CDVInvokedUrlCommand*)command;@end
# Import "myplugin. H "# import <Cordova/CDV. h> @ implementation myplugin-(void) add :( cdvinvokedurlcommand *) command {cdvpluginresult * pluginresult = nil; nsstring * JavaScript = nil; @ try {nsstring * echo = [command. arguments objectatindex: 0]; nsstring * echo2 = [command. arguments objectatindex: 1]; If (echo! = Nil & [Echo length]> 0 & echo2! = Nil & [echo2 length]> 0) {/*** add string */nsstring * addresult = [nsstring stringwithformat: @ "% @", Echo, echo2]; pluginresult = [cdvpluginresult resultwithstatus: cdvcommandstatus_ OK messageasstring: addresult]; JavaScript = [pluginresult tosuccesscallbackstring: Command. callbackid];} else {pluginresult = [cdvpluginresult resultwithstatus: cdvcommandstatus_error]; JavaScript = [pluginresult toerrorcallbackstring: Command. callbackid] ;}}@ catch (nsexception * exception) {pluginresult = [cdvpluginresult resultwithstatus: Invalid messageasstring: [Exception reason]; JavaScript = [pluginresult toerrorcallbackstring: Command. callbackid];} [self writejavascript: javascript];} @ end
3. create and implement myplugin. js under the WWW-> JS folder
var MyPlugin = {add: function(args,args2,addSuc,addFaild) { cordova.exec(addSuc, addFaild, "MyPlugin", "add", [args,args2]);}};
4.modify index.html
<! Doctype HTML> <! -- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. see the notice file distributed with this work for additional information regarding copyright ownership. the ASF licenses this file to you under the Apache license, version 2.0 (the "License"); you may not use this file license t in compliance with the license. you may obtain a copy of the license athttp: // your unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "as is" basis, without warranties or conditions of any kind, either express or implied. see the license for the specific language governing permissions and limitations under the license. --> <HTML>
5. Configure Cordova. plist
Create myplugin = myplugin in plugins
6. Run