从Objective C委托回调到Cordova插件。

时间:2020-12-01 20:28:36

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.

情境:我正在构建一个cordova插件来连接现有的iOS库和Ionic。方法的基本映射已经完成并工作,这意味着我可以通过角度方法调用iOS方法并获得成功/错误回调。

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.

问题:有一个登录方法被调用,然后调用适当的委托方法(例如userDidLoginWithSuccess)。从登录方法回调到cordova插件很容易,但是我需要从委托方法中以某种方式回调,以便让Ionic应用程序知道用户是否成功登录。

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

PS:我看了这篇文章,没有任何帮助,虽然这是一个类似的问题。Phonegap——在插件委托中从Objective-c向Javascript发送消息

1 个解决方案

#1


5  

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

在.h中创建一个属性callbackId,用于存储插件的回调标识符

@property (nonatomic, strong) NSString* callbackId;

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

然后,在您的插件方法上,将callbackId存储在您创建的属性上。

self.callbackId = command.callbackId;

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

最后,在委托中使用callbackId发送插件结果。

CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"string result"];
[self.commandDelegate sendPluginResult:result callbackId:self.callbackId];

#1


5  

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

在.h中创建一个属性callbackId,用于存储插件的回调标识符

@property (nonatomic, strong) NSString* callbackId;

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

然后,在您的插件方法上,将callbackId存储在您创建的属性上。

self.callbackId = command.callbackId;

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

最后,在委托中使用callbackId发送插件结果。

CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"string result"];
[self.commandDelegate sendPluginResult:result callbackId:self.callbackId];