How do you access a KrollCallback in swift?

66 views Asked by At

I have implemented a custom framework and it is integrated into a hyperloop enabled project.

I am passing function definitions as arguments to a swift function that takes a protocol.

Javascript:

var customListener = {};

customListener.onPayEvent = function(event) {
    console.log("moop");
  };

var PayView = require('Pay/PayView');

var payView = PayView.alloc().initWithPayEventListener(customListener);

This javascript function definition comes in as a KrollCallback.

enter image description here

Swift Code:

class PayListener: NSObject, WootPayEventListener {
    let payEventListener: PayEventListener
    
    init(payEventListener: PayEventListener) {
        self.payEventListener = payEventListener
    }
    
    public func onPayEvent(PayEvent: PayEvent) {

        os_log("calling payEventListener.onPayEvent")
        os_log("listener description = %{public}@", self.payEventListener.description)

        os_log("listener debugDescription = %{public}@", self.payEventListener.debugDescription ?? "")
//        self.payEventListener.onPayEvent(payEvent: "woo dogggy")
    }
}

How do I call methods on this object so that I can return the result from swift back to javascript?

1

There are 1 answers

0
dazza5000 On BEST ANSWER

I was able to do this by building the TitaniumKit framework locally and then importing it into my project.

The TitaniumKit source code is here: https://github.com/appcelerator/titanium_mobile/tree/master/iphone/TitaniumKit

The current build steps for the framework are below

carthage build --archive

Once I imported it into the project I was able to use KrollCallback like this:

class SimplePayListener: NSObject, SimplePayEventListener {
    let payEventListener: PayEventListener
    
    init(payEventListener: PayEventListener) {
        self.payEventListener = payEventListener
    }
    
    public func onPayEvent(payEvent_ payEvent: String) {
        os_log("SimplePayListener event description = %{public}@", fivestarsPayEvent.description)

        let appceleratorCallback:KrollCallback = self.payEventListener as! KrollCallback
        
        appceleratorCallback.call([payEvent], thisObject: self.payEventListener)
    }
}