从Apple TV TVML TVJS JavaScript应用程序调用Swift代码 [英] Call Swift code from an Apple TV TVML TVJS JavaScript App

查看:160
本文介绍了从Apple TV TVML TVJS JavaScript应用程序调用Swift代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了基本的TVML应用程序.当前,简单事件(例如按钮按下)是通过JavaScript(TVJS)处理的.

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

当用户按下按钮(通过TVML模板提供)时,我希望一些代码可以在Swift中运行,从而操纵UI元素.

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?

推荐答案

您可以按以下方式在TVApplicationControllerDelegate中使用EvaluationAppJavaScriptIn方法,并在其中编写相应的swift方法; (敏捷的一面)

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

之后,您可以像这样从TVJS调用此方法; (js端)

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

debug('Hello from js to swift...');

这篇关于从Apple TV TVML TVJS JavaScript应用程序调用Swift代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆