Tim Dietrich

Custom Software Developer

Home Services Portfolio Blog About Contact Newsletter

Developing Apple TV Apps With Embedded TVML and TVJS Files

When developing TVML apps, it's sometimes helpful to use embedded TVJS and TVML files. I wouldn't recommend using this technique for production apps, as it eliminates the benefits that the normal client-server behavior gives us. However, it can help to reduce development time.

The technique involves loading the TVJS and TVML files directly from the application bundle. It sets the TVBootURL launch option by getting an NSURL to the "Application.js" file. It uses that NSURL, along with the "URLByDeletingLastPathComponent" method, to set the TVBaseURL launch option. The TVBaseURL needs to be a string, so the trick (if you can call it one) is to cast the NSURL as a string, and force unwrap it.

Here's a snippet of code that shows the TVBootURL and TVBaseURL properties being set.

static let TVBootURL = NSBundle.mainBundle().URLForResource("Application", withExtension: "js")
static let TVBaseURL = String(TVBootURL!.URLByDeletingLastPathComponent!)

Here's another snippet that shows the didFinishLaunchingWithOptions function.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)

let appControllerContext = TVApplicationControllerContext()

appControllerContext.javaScriptApplicationURL = AppDelegate.TVBootURL!

appControllerContext.launchOptions["BASEURL"] = AppDelegate.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

A complete AppDelegate file that demonstrates this technique is available here.