首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >你能在苹果电视上而不是外部服务器上托管TVJS文件吗?

你能在苹果电视上而不是外部服务器上托管TVJS文件吗?
EN

Stack Overflow用户
提问于 2015-10-29 02:35:05
回答 1查看 2.1K关注 0票数 4

我已经从苹果下载了TVMLCatalog应用程序。代码分为两部分。

  1. 客户端-这包含TVML和TVJS文件。
  2. TVMLCatalog Project --这是设置TVML/TVJS的基本Xcode项目。

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

我对AppDelegate didFinishLaunching做了如下更改:

代码语言:javascript
复制
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
}

下面是演示如何导入clientXcode屏幕截图的屏幕截图

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

启动应用程序错误-操作无法完成。(TVMLKitErrorDomain错误3.)

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-29 03:57:43

我在谷歌搜索了一段时间后找到了答案。这个人的帖子真的帮了我:

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

这个例子在objective中,但是我已经实现了一个Swift解决方案。

下面是我如何从最初的文章中更改代码:

代码语言:javascript
复制
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.
    */

    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, value) in launchOptions {
            appControllerContext.launchOptions[kind] = value
        }
    }

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

    return true
}

注意:您需要更改TVJS文件中的文件路径引用,以反映新的包路径结构。

Application.js中的示例:

代码语言:javascript
复制
App.onLaunch = function(options) {
var javascriptFiles = [
    `${options.BASEURL}js/ResourceLoader.js`,
    `${options.BASEURL}js/Presenter.js`
];
...

变成:

代码语言:javascript
复制
App.onLaunch = function(options) {
var javascriptFiles = [
    `${options.BASEURL}ResourceLoader.js`,
    `${options.BASEURL}Presenter.js`
];
...

这条路:

${options.BASEURL}templates/Index.xml.js

变成:

${options.BASEURL}Index.xml.js

更新

Swift 3

重要事项:将您的application.js文件添加到项目的目标中;在启动新项目时,默认情况下不会添加该文件。

代码语言:javascript
复制
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, window: window, delegate: self)

    return true
}
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33404995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档