为这个设置挠头的一天。我觉得编写插件代码比设置要容易得多。我按照以下步骤为 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>
现在,当我单击按钮时,不会调用本机函数。如何前进?