【问题标题】:Phonegap fire custom events on iOSiOS 上的 Phonegap 触发自定义事件
【发布时间】: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


    【解决方案1】:

    根据您的代码,我猜您使用的是 PhoneGap / Cordova 1.5.0,对吧? 我之所以问,是因为 1.6.0 版本中更改了很多代码。

    我认为下面这行是不正确的:

    self.callbackID = [arguments objectAtIndex:0];
    

    您可以通过调试字符串值自行检查。 根据我的信息,应使用以下语句为您的结果对象检索正确的回调 id:

    self.callbackID = [arguments pop];
    

    另外,请确保您已在 Cordova.plist 中注册您的插件。 希望这能解决您的问题。

    最好, 马丁

    【讨论】:

      【解决方案2】:

      您的 js 看起来确实有点奇怪 - 我建议您按照本教程进行操作(或者直接下载项目并从那里获取;-)

      http://www.adobe.com/devnet/html5/articles/extending-phonegap-with-native-plugins-for-ios.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-09
        • 1970-01-01
        • 2023-03-10
        • 2013-11-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多