Phonegap for IOS Plugin插件开发

1.新建Phonegap工程MyPlugin

环境

xcode4.5.1

phonegap2.1.0

2.在Plugin文件夹下创建实现Plugin子类

 

[plain]  view plain copy print ?
 
  1. //  
  2. //  MyPlugin.h  
  3. //  MyPGPlugin  
  4. //  
  5. //  Created by kllmctrl on 12-10-29.  
  6. //  
  7. //  
  8.   
  9. #import <Cordova/CDV.h>  
  10.   
  11. @interface MyPlugin : CDVPlugin  
  12.   
  13. - (void) add:(CDVInvokedUrlCommand*)command;  
  14.   
  15. @end  
[plain]  view plain copy print ?
 
  1. #import "MyPlugin.h"  
  2. #import <Cordova/CDV.h>  
  3.   
  4. @implementation MyPlugin  
  5.   
  6. - (void) add:(CDVInvokedUrlCommand*)command{  
  7.     CDVPluginResult* pluginResult = nil;  
  8.     NSString* javaScript = nil;  
  9.       
  10.     @try {  
  11.           
  12.         NSString* echo = [command.arguments objectAtIndex:0];  
  13.         NSString* echo2 = [command.arguments objectAtIndex:1];  
  14.           
  15.         if (echo != nil  
  16.             && [echo length] > 0  
  17.             && echo2 != nil  
  18.             && [echo2 length] > 0) {  
  19.               
  20.             /**  
  21.              *相加字符串  
  22.              */  
  23.             NSString *addResult = [NSString stringWithFormat:@"%@%@", echo, echo2];  
  24.             pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:addResult];  
  25.             javaScript = [pluginResult toSuccessCallbackString:command.callbackId];  
  26.               
  27.               
  28.         } else {  
  29.             pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];  
  30.             javaScript = [pluginResult toErrorCallbackString:command.callbackId];  
  31.         }  
  32.     } @catch (NSException* exception) {  
  33.         pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];  
  34.         javaScript = [pluginResult toErrorCallbackString:command.callbackId];  
  35.     }  
  36.       
  37.     [self writeJavascript:javaScript];  
  38. }  
  39.   
  40. @end  

3.在www->js文件夹下创建实现 MyPlugin.js

 

[plain]  view plain copy print ?
 
  1. var MyPlugin = {  
  2. add: function(args,args2,addSuc,addFaild) {  
  3.     cordova.exec(addSuc, addFaild, "MyPlugin", "add", [args,args2]);  
  4. }  
  5. };  



 

4.修改index.html

 

[html]  view plain copy print ?
 
  1. <!DOCTYPE html>  
  2. <!--  
  3.  Licensed to the Apache Software Foundation (ASF) under one  
  4.  or more contributor license agreements.  See the NOTICE file  
  5.  distributed with this work for additional information  
  6.  regarding copyright ownership.  The ASF licenses this file  
  7.  to you under the Apache License, Version 2.0 (the  
  8.  "License"); you may not use this file except in compliance  
  9.  with the License.  You may obtain a copy of the License at  
  10.    
  11.  http://www.apache.org/licenses/LICENSE-2.0  
  12.    
  13.  Unless required by applicable law or agreed to in writing,  
  14.  software distributed under the License is distributed on an  
  15.  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  
  16.  KIND, either express or implied.  See the License for the  
  17.  specific language governing permissions and limitations  
  18.  under the License.  
  19.  -->  
  20. <html>  
  21.     <head>  
  22.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  23.         <meta name="format-detection" content="telephone=no" />  
  24.         <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />  
  25.         <link rel="stylesheet" type="text/css" href="css/index.css" />  
  26.         <title>kllmctrl</title>  
  27.     </head>  
  28.     <body>  
  29.         <div class="app">  
  30.             <h1>Apache Cordova kllmctrl</h1>  
  31.             <div id="deviceready" class="blink">  
  32.                 <p class="event listening">Connecting to Device</p>  
  33.                 <p class="event received">Device is Ready</p>  
  34.             </div>  
  35.         </div>  
  36.         <script type="text/javascript" src="cordova-2.1.0.js"></script>  
  37.         <script type="text/javascript" src="js/MyPlugin.js"></script>  
  38.         <script type="text/javascript">  
  39.         document.addEventListener("deviceready", onDeviceReady, false);  
  40.         function onDeviceReady() {  
  41.             /**  
  42.              * 加载成功调用js  
  43.              */  
  44.             MyPlugin.add("My","Plugin",addSuc,addFiald);  
  45.         }  
  46.         </script>  
  47.           
  48.         <script type="text/javascript">  
  49.             /**  
  50.              * js回调函数  
  51.              */  
  52.             function addSuc(result) {  
  53.                 console.log('addSuc='+result);  
  54.             }  
  55.             function addFiald() {  
  56.                 console.log('addFiald');  
  57.             }  
  58.             </script>  
  59.     </body>  
  60. </html>  


5.配置Cordova.plist

 

在Plugins建MyPlugin=MyPlugin

 

6.run

 

phonegap插件下载地址:https://github.com/phonegap/phonegap-plugins/

猜你喜欢

转载自lishuaishuai.iteye.com/blog/1965492