Phonegap for IOS Plugin插件开发

news/2024/9/5 19:30:39 标签: javascript, 移动开发, xcode

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/


http://www.niftyadmin.cn/n/591751.html

相关文章

UIButton---iOS

1.button更改按下的背景图片 [btn setImage:bgImg1 forState:UIControlStateHighlighted]; 设置Button的字体大小&#xff1b; btn.titleLabel.font [UIFont systemFontOfSize: 14.0]; UIButton去掉系统的按下高亮置灰效果 [downImageV setAdjustsImageWhenHighlighted:NO]; U…

Android library projects cannot be launched.

解决Andriod中编译时出现“Andrior library projects cannot be launched.”的问题。 问题截图如下&#xff1a; 解决问题截图如下&#xff1a;

IOS中http请求使用cookie

一直以为ios的http请求这块很简单应该不支持记录&#xff0c;保存&#xff0c;或者使用cookie&#xff0c;可是想当然归想当然&#xff0c;真用的时候&#xff0c;真研究了一下发现还真强大。经过一番的研究简单说一下我的理解&#xff1a;当你访问一个网站时&#xff0c;不管你…

icon去除高亮

在plist文件里面设置这两项 转载于:https://my.oschina.net/u/1441580/blog/283954

SpringBoard 无法启动应用程序(错误:6)

转载于:https://my.oschina.net/u/1441580/blog/283953

怎样在android中添加背景图片?

怎样在android中添加背景图片&#xff1f; 1.打开左侧的工程文件夹。打开res中的layout&#xff0c;双击main.xml。 2.一般的图片是jpg格式的,要把格式转化为png格式的。 3.把PNG格式的图片拖到工程中res中的drawable-hdpi中 4.在main.xml中的LinearLayout下面&#xff0c…

NSHTTPCookieStorage

从NSObject继承符合NSObject的&#xff08;NSObject的&#xff09;框架/系统/资源库/框架/ Foundation.framework可用性&#xff0c;可在Mac OS X V10.2安装使用Safari 1.0。 在Mac OS X v10.2.7和更高的市场。 在NSHTTPCookieStorage.h宣布伴侣指南URL加载系统 概述 NSHTTPCoo…

iOS面试中遇到@synthesize和@dynamic

synthesize 除非开发人员已经做了&#xff0c;否则由编译器自动生成getter/setter方法。 当开发人员自定义存或取方法时&#xff0c;自定义会屏蔽自动生成该方法。 dynamic 告诉编译器&#xff0c;不自动生成getter/setter方法&#xff0c;避免编译期间产生警告。 是由开发人…