【发布时间】:2012-03-26 07:38:12
【问题描述】:
我正在尝试在 iOS 上的 phonegap 中触发自定义事件。好的,我到目前为止所做的:
我创建了一个自定义插件,我可以从 Javascript 调用我的插件并且一切正常。基本上,该插件显示一个 ModalViewController 呈现一些本机功能
例如录制和编辑视频,一旦用户完成,我会将视频上传到 youtube。我想在下载完成后触发一个事件,但目前我无法执行此操作。
这是我使用的代码的一部分:
在我的 index.html 中,我通过单击按钮触发了此功能,(我不是 Javascript 开发人员)nativeFunction 基本上调用了我的自定义插件。
function testCustomPlugin()
{
MyClass.nativeFunction(
function(result) {
alert("Success : \r\n"+result);
},
function(error) {
alert("Error : \r\n"+error);
}
);
document.addEventListener("post_sent",onPostSent,false);
}
function onPostSent()
{
alert("post sent");
}
这是我的 MyClass.js :
var MyClass = {
nativeFunction: function(types, success, fail) {
return Cordova.exec(success, fail, "MyClass", "showInterface", types);
}
}
在 MyClass.m 中我有两个方法:showInterface 和 sendNotification,
showInterface.m
- (void) showInterface:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
self.callbackID = [arguments objectAtIndex:0];
// missing code
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sendSuccessNotification:)
name:@"PostSentNotification" object:nil];
}
-(void)sendSuccessNotification:(NSNotification *)notification
{
if (self.callbackID) {
NSLog(@"%@",callbackID);
NSLog(@"sendSuccessNotification");
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"testCallBack"
forKey:@"returnValue"];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:dictionary
[result setKeepCallbackAsBool:YES];
[super writeJavascript:[result toSuccessCallbackString:self.callbackID]];
}
}
我可以在我的日志中看到 sendSuccessNotification 被调用但事件没有被触发,我确信我在 javascript 中做错了,但问题是我不知道是什么。
在此先感谢您的帮助
【问题讨论】:
标签: ios events plugins cordova