您可以在 Apple TV 而不是外部服务器上托管 TVJS 文件吗?
Can you host TVJS files on the Apple TV instead of an external server?
我已经从 Apple 下载了 TVMLCatalog 应用程序。代码分为两部分。
- client - 这包含 TVML 和 TVJS 文件
- TVMLCatalog 项目 - 这是设置 TVML/TVJS
的基本 Xcode 项目
我试图在与 TVMLCatalog 项目.
相同的包中托管 client TVJS 文件
我已将 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 文件吗?
经过深入谷歌搜索,我找到了答案。这个人的post真的帮了我:
http://thejustinwalsh.com/objective-c/tvml/2015/09/20/tvml-without-the-webserver.html
示例在 objective-c 中,但我已经实施了 Swift 解决方案。
以下是我如何更改原始代码 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 文件中的文件路径引用以反映新的包路径结构。
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
[更新]
Swift 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, window: window, delegate: self)
return true
}
我已经从 Apple 下载了 TVMLCatalog 应用程序。代码分为两部分。
- client - 这包含 TVML 和 TVJS 文件
- TVMLCatalog 项目 - 这是设置 TVML/TVJS 的基本 Xcode 项目
我试图在与 TVMLCatalog 项目.
相同的包中托管 client TVJS 文件我已将 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 文件吗?
经过深入谷歌搜索,我找到了答案。这个人的post真的帮了我:
http://thejustinwalsh.com/objective-c/tvml/2015/09/20/tvml-without-the-webserver.html
示例在 objective-c 中,但我已经实施了 Swift 解决方案。
以下是我如何更改原始代码 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 文件中的文件路径引用以反映新的包路径结构。
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
[更新]
Swift 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, window: window, delegate: self)
return true
}