问题描述
我已经从Apple下载了TVMLCatalog应用程序.该代码分为两部分.
I've downloaded the TVMLCatalog application from Apple. The code is split up into 2 parts.
- 客户端-包含TVML和TVJS文件
- TVMLCatalog项目-这是设置TVML/TVJS的基本Xcode项目
- client - this holds the TVML and TVJS files
- TVMLCatalog Project - this is the basic Xcode project that sets up the TVML/TVJS
我正尝试将客户端 TVJS文件与 TVMLCatalog项目放在同一包中.
I'm attempting to host the client TVJS files in the same bundle as the TVMLCatalog Project.
我已如下更改AppDelegate didFinishLaunching:
I've changed the AppDelegate didFinishLaunching as follows:
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屏幕截图
Here is a screenshot that demonstrates how I've imported the client:Xcode Screenshot
运行项目(仅在模拟器上进行测试)时,在AppleTV模拟器屏幕上显示以下消息:
When I run the project (only tested on simulator) I get the following message displayed on the AppleTV simulator screen:
我可以像这样从本地加载TVJS文件吗?
Can I load from TVJS files locally like this?
推荐答案
经过深入的谷歌搜索,我找到了答案.这个人的帖子确实对我有帮助:
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
该示例在Objective-C中,但是我已经实现了Swift解决方案.
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
}
一个重要的注意事项:您需要更改TVJS文件中的文件路径引用,以反映新的包路径结构.
One important note: You'll need to change the filepath references in your TVJS files to reflect the new bundle path structure.
Application.js中的示例:
example in 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}templates/Index.xml.js
成为:
${options.BASEURL}Index.xml.js
[UPDATE]
快捷键3
重要提示:将您的application.js文件添加到项目的目标;启动新项目时,默认情况下不会添加此代码.
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
}
这篇关于您可以在Apple TV而不是外部服务器上托管TVJS文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!