这是一天为此设置而烦恼的一天。我觉得编写插件代码比设置容易得多。我按照以下步骤为ios编写了一个简单的手机间隙插件。但不幸的是,无法找到我错过的地方。请打印出代码中缺失/混淆/错误的部分。
PhoneGap设置:
1)关闭Xcode。 [我有Xcode 4.3.3] 2)下载最新的手机缺口。版本2.0 3)在phonegap-phonegap-2dbbdab-1目录中, 安装Cordova-2.0.0.pkg
4)运行以下代码:
$ ./path/to/cordova-ios/bin/create / path / to / my_new_cordova_project com.example.cordova_project_name CordovaProjectName
其次: http://docs.phonegap.com/en/2.0.0/guide_command-line_index.md.html#Command-Line%20Usage
5)打开Xcode项目。
6)在包含..
的www目录下创建了HelloPlugin.js文件 var HelloPlugin = {
callNativeFunction: function (success, fail, resultType) {
return Cordova.exec(success, fail, "com.tricedesigns.HelloPlugin", "nativeFunction", [resultType]);
}
};
7)插入目录
HelloPlugin.h 包含:
#import "CDVPlugin.h"
@interface HelloPlugin : CDVPlugin {
NSString* callbackID;
}
@property (nonatomic, copy) NSString* callbackID;
- (void) nativeFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
HelloPlugin.m包含:
#import "HelloPlugin.h"
@implementation HelloPlugin
@synthesize callbackID;
- (void) nativeFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
//get the callback id
NSString *callbackId = [arguments pop];
NSLog(@"Hello, this i s 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
Cordova.plist中的: 添加了 com.tricedesigns.HelloPlugin,HelloPlugin作为键/值,类型是字符串。
index.html中的:
<script type="text/javascript" charset="utf-8" src="HelloPlugin.js"></script>
<script type="text/javascript">
function callNativePlugin( returnSuccess ) {
alert("Inside callNativePlugin");
HelloPlugin.callNativeFunction( nativePluginResultHandler, nativePluginErrorHandler, returnSuccess );
alert("End of Hello PLugin");
}
function nativePluginResultHandler (result) {
alert("SUCCESS: \r\n"+result );
}
function nativePluginErrorHandler (error) {
alert("ERROR: \r\n"+error );
}
</script>
现在,当我单击按钮时,不会调用本机函数。如何前进?