2015-10-29 42 views
4

我已經從Apple下載了TVMLCatalog應用程序。代碼分爲兩部分。您可以在Apple TV上代替外部服務器託管TVJS文件嗎?

  1. 客戶 - 這個持有TVML和TVJS文件
  2. TVMLCatalog項目 - 這是基本的Xcode項目,設置了TVML/TVJS

我試圖主機客戶端 TVJS文件與TVMLCatalog項目相同。

我已經改變了的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模擬器屏幕上顯示以下消息:

啓動應用程序時出錯 - 操作無法完成。 (TVMLKitErrorDomain錯誤3.)

我可以像這樣從本地加載TVJS文件嗎?

回答

8

經過一番深入的谷歌搜索之後,我找到了答案。此人的職位真的幫了我:

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

的例子是在Objective-C,但我實現了迅速解決。

這裏是我改變了我的代碼從原來的文章:

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文件中的文件路徑引用,以反映新的軟件包路徑結構。

應用程序中的示例。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

[更新]

斯威夫特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 
} 
+0

在我看來,以'if let launchOptions..'開頭的5行是不需要的 - 或者至少,它沒有它們就可以爲我工作。 – coco

+0

完全不起作用。這是什麼版本 – Coldstar

+0

@Coldstar更新了Swift 3。 – shirefriendship

相關問題