I’m Trying to fire a custom event in phonegap on iOS. Ok what I’ve done so far :
I’ve created a custom plugin , I’m able to call my plugin from Javascript and all work properly. Basically the plugin show a ModalViewController presenting some native functionality
such as recording and editing a video, once the user has finished I will upload the video to youtube. I would like to fire an event when the download is completed but at the moment I wasn’t able to do this.
This is part of the code I use :
In my index.html I have this function triggered by a click on a button, (I’m not a Javascript developer) nativeFunction basically call my custom plugin.
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");
}
This is my MyClass.js :
var MyClass = {
nativeFunction: function(types, success, fail) {
return Cordova.exec(success, fail, "MyClass", "showInterface", types);
}
}
Inside MyClass.m I have two methods : showInterface and 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]];
}
}
I can see in my log that sendSuccessNotification is called but the event is not fired, I am sure that I do something wrong in the javascript but the problem is that I don’t know what.
Thanks in advance for any help
According to your code, I guess you are using PhoneGap / Cordova 1.5.0, right?
I’m asking because a lot of code has been changed in version 1.6.0.
In my opinion the following line is incorrect:
You can check this yourself by debugging the string value.
According to my information the following statement should be used to retrieve the correct callback id for your result object:
Also, please make sure that you have registered your plugin in your Cordova.plist.
Hope this solves your issue.
Best,
Martin