Can you host TVJS files on the Apple TV instead of an external server?

烂漫一生 提交于 2019-12-05 13:25:02

I was able to find the answer after some deep googling. This person's post really helped me:

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

The example is in objective-c but I've implemented a Swift solution.

Here is how I changed my code from the original post:

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
}

One important note: You'll need to change the filepath references in your TVJS files to reflect the new bundle path structure.

example in Application.js:

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

becomes:

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

and this path:

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

becomes:

${options.BASEURL}Index.xml.js

[UPDATE]

Swift 3

Important: Add your application.js file to the project's target; this is not added by default when starting a new project.

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
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!