且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Apple TV TVML TVJS JavaScript应用程序调用Swift代码

更新时间:2023-12-03 17:41:28

您可以按以下方式在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...');