Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback to Cordova plugin from Objective C delegate

Situation: I'm building a cordova plugin to connect an existing iOS library with Ionic. The base mapping of methods is done and working, means I can call the iOS methods via Angular methods and getting success/error callbacks.

Problem: There's a login method which is called, and appropriate delegate methods (e.g. userDidLoginWithSuccess) are called afterwards. Callback from the login method to the cordova plugin is easy, but I need to somehow callback from the delegate method, in order to let the Ionic app know if the user was logged in successfully or not.

Any thoughts on this? Thanks.

PS: I checked this post, which didn't help, although it's a similar question. Phonegap - Send message to Javascript from Objective-c in a plugin delegate

like image 542
Dani Avatar asked Feb 09 '23 23:02

Dani


1 Answers

On your .h create a property callbackId that you will use to store the callback identifier of the plugin

@property (nonatomic, strong) NSString* callbackId;

Then, on your plugin method, store the callbackId on the property you created.

self.callbackId = command.callbackId;

and finally, on the delegate send the plugin result using the callbackId

CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"string result"];
[self.commandDelegate sendPluginResult:result callbackId:self.callbackId];
like image 153
jcesarmobile Avatar answered Feb 11 '23 16:02

jcesarmobile