加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xcode – 您可以在Apple TV而不是外部服务器上托管TVJS文件吗?

发布时间:2020-12-14 18:07:24 所属栏目:百科 来源:网络整理
导读:我从Apple下载了TVMLCatalog应用程序.代码分为两部分. 客户端 – 它包含TVML和TVJS文件 TVMLCatalog项目 – 这是设置TVML / TVJS的基本Xcode项目 我正在尝试将客户端TVJS文件托管在与TVMLCatalog项目相同的包中. 我已经改变了AppDelegate didFinishLaunching
我从Apple下载了TVMLCatalog应用程序.代码分为两部分.

>客户端 – 它包含TVML和TVJS文件
> TVMLCatalog项目 – 这是设置TVML / TVJS的基本Xcode项目

我正在尝试将客户端TVJS文件托管在与TVMLCatalog项目相同的包中.

我已经改变了AppDelegate didFinishLaunching如下:

func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    window = UIWindow(frame: UIScreen.mainScreen().bounds)

    /*
    Create the TVApplicationControllerContext for this application
    and set the properties that will be passed to the `App.onLaunch` function
    in JavaScript.
    */
    let appControllerContext = TVApplicationControllerContext()

    /*
    The JavaScript URL is used to create the JavaScript context for your
    TVMLKit application. Although it is possible to separate your JavaScript
    into separate files,to help reduce the launch time of your application
    we recommend creating minified and compressed version of this resource.
    This will allow for the resource to be retrieved and UI presented to
    the user quickly.
    */

    TVBootURL = NSBundle.mainBundle().pathForResource("application",ofType: "js")!
    TVBaseURL = TVBootURL.stringByReplacingOccurrencesOfString("application.js",withString: "")
    if let javaScriptURL = NSURL(string: TVBootURL) {
        appControllerContext.javaScriptApplicationURL = javaScriptURL
    }

    appControllerContext.launchOptions["BASEURL"] = TVBaseURL

    if let launchOptions = launchOptions as? [String: AnyObject] {
        for (kind,value) in launchOptions {
            appControllerContext.launchOptions[kind] = value
        }
    }

    appController = TVApplicationController(context: appControllerContext,window: window,delegate: self)

    return true
}

这是一个截图,演示了我如何导入客户端:
Xcode Screenshot

当我运行项目(仅在模拟器上测试)时,我在AppleTV模拟器屏幕上显示以下消息:

Error launching Application – The operation couldn’t be completed. (TVMLKitErrorDomain error 3.)

我可以像这样从本地加载TVJS文件吗?

解决方法

经过一些深度的谷歌搜索,我能够找到答案.这个人的帖子真的帮到了我:

http://thejustinwalsh.com/objective-c/tvml/2015/09/20/tvml-without-the-webserver.html

示例是在objective-c中,但我实现了一个Swift解决方案.

以下是我从原帖中更改代码的方法:

func application(application: UIApplication,to help reduce the launch time of your application
    we recommend creating minified and compressed version of this resource.
    This will allow for the resource to be retrieved and UI presented to
    the user quickly.
    */

    if let javaScriptURL = NSBundle.mainBundle().URLForResource("application",withExtension: "js"){
        appControllerContext.javaScriptApplicationURL = javaScriptURL
    }

    let TVBaseURL = appControllerContext.javaScriptApplicationURL.URLByDeletingLastPathComponent

    appControllerContext.launchOptions["BASEURL"] = TVBaseURL?.absoluteString

    if let launchOptions = launchOptions as? [String: AnyObject] {
        for (kind,delegate: self)

    return true
}

一个重要注意事项:您需要更改TVJS文件中的文件路径引用以反映新的包路径结构.

Application.js中的示例:

App.onLaunch = function(options) {
var javascriptFiles = [
    `${options.BASEURL}js/ResourceLoader.js`,`${options.BASEURL}js/Presenter.js`
];
...

变为:

App.onLaunch = function(options) {
var javascriptFiles = [
    `${options.BASEURL}ResourceLoader.js`,`${options.BASEURL}Presenter.js`
];
...

而这条道路:

${} options.BASEURL模板/ Index.xml.js

变为:

${} options.BASEURL Index.xml.js

[UPDATE]

斯威夫特3

重要提示:将application.js文件添加到项目的目标中;启动新项目时,默认情况下不会添加此项.

func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window = UIWindow(frame: UIScreen.main.bounds)

    // Create the TVApplicationControllerContext for this application and set the properties that will be passed to the `App.onLaunch` function in JavaScript.
    let appControllerContext = TVApplicationControllerContext()

    // The JavaScript URL is used to create the JavaScript context for your TVMLKit application. Although it is possible to separate your JavaScript into separate files,to help reduce the launch time of your application we recommend creating minified and compressed version of this resource. This will allow for the resource to be retrieved and UI presented to the user quickly.
    if let javaScriptURL = Bundle.main.url(forResource: "application",withExtension: "js"){
        appControllerContext.javaScriptApplicationURL = javaScriptURL
    }

    let TVBaseURL = appControllerContext.javaScriptApplicationURL.deletingLastPathComponent()

    appControllerContext.launchOptions["BASEURL"] = TVBaseURL.absoluteString

    if let launchOptions = launchOptions {
        for (kind,value) in launchOptions {
            appControllerContext.launchOptions[kind.rawValue] = value
        }
    }

    appController = TVApplicationController(context: appControllerContext,delegate: self)

    return true
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读