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?

4

1 に答える 1

2

以下のように TVApplicationControllerDelegate で evaluateAppJavaScriptIn メソッドを使用し、それに対応する迅速なメソッドを記述できます。(スイフト側)

// 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)!)
}

その後、次のように TVJS からこのメソッドを呼び出すことができます。(js側)

debug('Hello from js to swift...');
于 2016-10-15T13:10:40.523 に答える