0

I have a basic TVML application set up. Currently simple events (such as a button press) are handled via JavaScript (TVJS).

When the user presses a button (provided via TVML template) I'd like some code to run in Swift instead, that manipulates UI elements.

What's the best way to do this?

Gaurav Sharma
  • 2,680
  • 3
  • 26
  • 36
  • 2
    See this: [How to use both native and TVML in a tvOS app?](http://stackoverflow.com/questions/37765886/how-to-use-both-native-and-tvml-in-a-tvos-app). – OOPer Jul 04 '16 at 18:06

1 Answers1

2

You can use evaluateAppJavaScriptIn method in TVApplicationControllerDelegate as below and write corresponding swift method in it; (swift side)

// MARK: TVApplicationControllerDelegate
func appController(_ appController: TVApplicationController, evaluateAppJavaScriptIn jsContext: JSContext){
    let debug : @convention(block) (String!) -> Void = {
        (string : String!) -> Void in
        #if DEBUG
            print("[log]: \(string!)\n")
        #endif
    }
    jsContext.setObject(unsafeBitCast(debug, to: AnyObject.self), forKeyedSubscript: "debug" as (NSCopying & NSObjectProtocol)!)
}

After that you can call this method from TVJS like this; (js side)

debug('Hello from js to swift...');
mpakkan
  • 46
  • 3