用上一节我们创建的Hello 项目来示例
http://www.mwebgl.com/dede/a/kaifakuangjia/Phonegapjishuwendang/2012/0511/125.html
在Hello项目中创建 js HelloPlugin.js
var HelloPlugin = { callNativeFunction: function (success, fail, resultType) {
return Cordova.exec(
success, fail, "com.tricedesigns.HelloPlugin",
"nativeFunction", [resultType]);
}
};
创建 Native 类
右击plugins文件新建文件
在新建的文件中选择 Objective-C class
命名 HelloPlugin 并且创建
点击下一步
在你的项目中新建了一个.h的文件 实现 .m文件的接口
在你的.h文件中写入一下代码,此代码为接口代码
#import <Cordova/CDV.h>
@interface HelloPlugin : CDVPlugin
- (void) nativeFunction:(NSMutableArray*)
arguments withDict:(NSMutableDictionary*)options;
@end
在你的.m文件中写入实现.h接口
#import "HelloPlugin.h"
@implementation HelloPlugin
- (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]]; } }
@end
在index.html中调用
<script type="text/javascript" charset="utf-8" src="HelloPlugin.js"></script>
调用一下函数
function callNativePlugin( returnSuccess ) {
HelloPlugin.callNativeFunction(
nativePluginResultHandler, nativePluginErrorHandler, returnSuccess );
}
function nativePluginResultHandler (result) {
alert("SUCCESS: \r\n"+result );
}
function nativePluginErrorHandler (error) {
alert("ERROR: \r\n"+error );
}
写入标记调用
<body οnlοad="onBodyLoad()">
<h1>Hey, it's Cordova!</h1>
<button οnclick="callNativePlugin('success');">SUCCESS!</button>
<button οnclick="callNativePlugin('error');">ERROR!</button>
</body>
在项目中打开 Cordova.plist.文件写入关联文件
key
保存你的更改并且运行项目
运行成功!
查看xcode的打印日志
http://www.mwebgl.com/dede/a/kaifakuangjia/Phonegapjishuwendang/2012/0511/125.html
在Hello项目中创建 js HelloPlugin.js
var HelloPlugin = { callNativeFunction: function (success, fail, resultType) {
return Cordova.exec(
success, fail, "com.tricedesigns.HelloPlugin",
"nativeFunction", [resultType]);
}
};
创建 Native 类
右击plugins文件新建文件
![Figure 1. Creating the new file.](http://mwebgl.gotoip4.com/dede/uploads/allimg/120511/203933F07-0.jpg)
在新建的文件中选择 Objective-C class
![Figure 2. Selecting the Object-C class template.](http://mwebgl.gotoip4.com/dede/uploads/allimg/120511/2039332927-1.jpg)
命名 HelloPlugin 并且创建
![Figure 3. Naming the class.](http://mwebgl.gotoip4.com/dede/uploads/allimg/120511/20393353M-2.jpg)
点击下一步
在你的项目中新建了一个.h的文件 实现 .m文件的接口
![Figure 4. The new native class files.](http://mwebgl.gotoip4.com/dede/uploads/allimg/120511/2039332493-3.png)
在你的.h文件中写入一下代码,此代码为接口代码
#import <Cordova/CDV.h>
@interface HelloPlugin : CDVPlugin
- (void) nativeFunction:(NSMutableArray*)
arguments withDict:(NSMutableDictionary*)options;
@end
在你的.m文件中写入实现.h接口
#import "HelloPlugin.h"
@implementation HelloPlugin
- (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]]; } }
@end
在index.html中调用
<script type="text/javascript" charset="utf-8" src="HelloPlugin.js"></script>
调用一下函数
function callNativePlugin( returnSuccess ) {
HelloPlugin.callNativeFunction(
nativePluginResultHandler, nativePluginErrorHandler, returnSuccess );
}
function nativePluginResultHandler (result) {
alert("SUCCESS: \r\n"+result );
}
function nativePluginErrorHandler (error) {
alert("ERROR: \r\n"+error );
}
写入标记调用
<body οnlοad="onBodyLoad()">
<h1>Hey, it's Cordova!</h1>
<button οnclick="callNativePlugin('success');">SUCCESS!</button>
<button οnclick="callNativePlugin('error');">ERROR!</button>
</body>
在项目中打开 Cordova.plist.文件写入关联文件
key
com.tricedesigns.HelloPlugin
和 value
"HelloPlugin"
![Figure 5. Editing Cordova.plist.](http://mwebgl.gotoip4.com/dede/uploads/allimg/120511/2039332395-4.jpg)
保存你的更改并且运行项目
![Figure 6. The application running in the iOS simulator.](http://mwebgl.gotoip4.com/dede/uploads/allimg/120511/2039334637-5.jpg)
运行成功!
查看xcode的打印日志
![Figure 7. Logged information in the Xcode debug console.](http://mwebgl.gotoip4.com/dede/uploads/allimg/120511/2039333939-6.png)