【发布时间】:2012-10-08 07:09:37
【问题描述】:
这就是我所做的:
1) 已安装 Cordova 2.0.0 版
2) 我的 XCode 版本是 4.3.3
3) 通过 ./create 命令创建了电话间隙项目。
4) 在index.html:
<script type="text/javascript">
function nativePluginResultHandler (result)
{
alert("SUCCESS: \r\n"+result );
}
function nativePluginErrorHandler (error)
{
alert("ERROR: \r\n"+error );
}
function callNativePlugin( returnSuccess )
{
alert("Invoking..");
HelloPlugin.callNativeFunction( nativePluginResultHandler, nativePluginErrorHandler, returnSuccess );
}
</script>
<h1>Hey, it's Cordova!</h1>
<button onclick="callNativePlugin('success');">Click to invoke the Native Plugin with an SUCCESS!</button>
<button onclick="callNativePlugin('error');">Click to invoke the Native Plugin with an ERROR!</button>
5)内部HelloPlugin.js:
var HelloPlugin = {
callNativeFunction: function (success, fail, resultType) {
echo "Welcome";
return Cordova.exec( success, fail, "com.mycompany.HelloPlugin", "nativeFunction", [resultType]);
} };
6) HelloPlugin.m:
#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
7) HelloPlugin.h:
#import "Cordova/CDVPlugin.h"
#import "Cordova/CDV.h"
@interface HelloPlugin : CDVPlugin
- (void) nativeFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
8) 在Cordova.plist 中,我添加了以下键/值:
com.mycompany.HelloPlugin HelloPlugin
问题是来自HelloPlugin 的本机函数根本没有被调用。
我在这里错过了什么?
非常感谢您的帮助。
【问题讨论】:
-
嗨!您是否尝试在 Cordova.plist 文件的插件部分添加
HelloPlugin和HelloPlugin,而不是com.mycompany.HelloPlugin和HelloPlugin? -
Littm,我试过了,但仍然没有调用。
-
您在控制台上是否遇到任何错误?
-
Littm,我没有收到任何错误 littm :(.
-
跟项目名/产品名/bundle id有关系吗?
标签: objective-c ios5 cordova ios4 phonegap-plugins