IOS phonegap的Plugins开发
来源:互联网 发布:空调品牌 知乎 编辑:程序博客网 时间:2024/05/16 05:32
参考地址:http://docs.phonegap.com/en/2.9.0/guide_plugin-development_ios_index.md.html#Developing%20a%20Plugin%20on%20iOS
1、新建插件文件:
2、Native端
Interactive.h
//// Interactive.h// middleware//// Created by bo on 13-7-1.////#import <Cordova/CDV.h>//有些老版本(貌似是phonegap 1.5.0之前)的引用是 #import <PhoneGap/PGPlugin.h>@interface Interactive : CDVPlugin//貌似是phonegap 1.5.0之前需要继承 PGPlugin/*旧的插件方法*/- (void) nativeFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;/*新的插件方法 ponegap 2.1.0版本开始*/- (void) myPluginMethod:(CDVInvokedUrlCommand*) command;@end
Interactive.m
//// Interactive.m// middleware//// Created by bo on 13-7-1.////#import "Interactive.h"@implementation Interactive- (void) nativeFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options { //get the callback id NSString *callbackId = [arguments pop]; NSLog(@"Hello, this is a native function called from PhoneGap/Cordova!"); NSString *resultType = [arguments objectAtIndex:0]; CDVPluginResult *result; if ( [resultType isEqualToString:@"success"] ) { result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @"Success :)"]; [self writeJavascript:[result toSuccessCallbackString:callbackId]]; } else { result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: @"Error :("]; [self writeJavascript:[result toErrorCallbackString:callbackId]]; }}- (void) myPluginMethod:(CDVInvokedUrlCommand*) command{ CDVPluginResult* pluginResult = nil; NSString* echo = [command.arguments objectAtIndex:0]; if (echo != nil && [echo length] > 0) { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo]; } else { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; } [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];}@end
3、plugins的配置
phonegap 2.1.0之前的配置方法:
找到PhoneGap.list (phonegap 1.5.0之前版本)或cordova.list(phonegap 1.5.0-2.1.0之间版本的配置),添加包名和js端需要引用的name名,分别为:com.ian.Interactive和Interactive,如图:
找到config.xml,在<plugins></plugins>内添加
<feature name="Interactive"> <param name="ios-package" value="Interactive" /> </feature>
<feature name="Interactive"> 这个“Interactive”是给js端引用时的一个name值
<param name="ios-package" value="Interactive">中,name值为"ios-package"是不变的,
value="Interactive" 这里的value值取的是继承CDVPlugin的类名,即Interactive.h中@interface Interactive : CDVPlugin 的 Interactive, 而不是像phonegap2.1.0之前版本一样取的是包名+类名
如图:
4、js端
//exec(<successFunction>,<failFunction>,<service(在config中配置的name)>,<action>,[<args>]); Cordova.exec( function(){ console.log("success!!!!!"); }, function(){ console.log("fail"); }, "Interactive", "myPluginMethod", ['success'])
到这里IOS端,js与native通过phonegap的插件进行相互调用配置结束。
下面是phonegap 2.8.1版本的 phonegap插件进行js与native的相互调用 范例
http://pan.baidu.com/share/link?shareid=2982847956&uk=1964506139 - IOS phonegap的Plugins开发
- 移动开发之phonegap-plugins-barcodescanner插件的试用
- Extending PhoneGap with native plugins for iOS (通过phonegap plugin的方式实现ios和javascript的互相调用)
- iOS 的Phonegap 插件开发 详解
- 搭建phonegap下的ios开发环境
- PhoneGap - iOS开发
- iOS 开发之 PhoneGap
- Phonegap iOS 插件开发
- PhoneGap教程:PhoneGap IOS应用开发入门
- PhoneGap plugins for Android
- phoneGap开发IOS,JS调用IOS方法/phoneGap插件开发
- IOS下Phonegap 插件开发
- PhoneGap插件开发示例(iOS)
- PhoneGap插件开发示例(iOS)
- iOS之PhoneGap开发环境
- PhoneGap入门 iOS 插件开发
- PhoneGap 2.0 iOS开发环境搭建可能遇到的issue
- PhoneGap 2.0 iOS开发环境搭建可能遇到的issue
- 不错的c++文章
- (转)项目经理如何有效地进行项目沟通?
- Google新放出的几个管理mysql的工具
- (转)android Notification 的使用
- spring mvc3+hibernate+mysql的一个不错的CRUD例子
- IOS phonegap的Plugins开发
- php tips小集
- Activity.managedQuery()
- Android tips3
- Android-Universal-Image-Loader 属性设置
- php中的变量函数
- Android中的soundpool小结
- PHP框架YII笔记之1
- (转)用mysql的存储过程和event来做计划任务