【问题标题】:tvOS not loading external TVML filetvOS 未加载外部 TVML 文件
【发布时间】:2015-10-22 18:43:01
【问题描述】:

在创建新的客户端-服务器 tvOS 应用程序时,tvOS 不会从我的外部 tvml 文件中获取数据。这是错误:ITML <Error>: Failed to load launch URL with error: (null)

这是main.js 代码

function getDocument(url) {
    var templateXHR = new XMLHttpRequest();
    templateXHR.responseType = "document";
    templateXHR.addEventListener("load", function() {pushDoc(templateXHR.responseXML);}, false);
    templateXHR.open("GET", url, true);
    templateXHR.send();
    return templateXHR;
}

function pushDoc(document) {
    navigationDocument.pushDocument(document);
}

App.onLaunch = function(options) {
    var templateURL = 'niclasblog.com/appletv/main.tvml';
    getDocument(templateURL);
}

App.onExit = function() {
    console.log('App finished');
}

我还附上了main.tvml 文件

<document>
   <alertTemplate>
      <title>Test</title>
      <description>This is a test</description>
      <button>
         <text>Yes</text>
      </button>
      <button>
         <text>No</text>
      </button>
   </alertTemplate>
</document>

该代码直接来自 Apple 文档,所以我不知道为什么它不起作用。

【问题讨论】:

    标签: javascript url tvos tvml


    【解决方案1】:

    在您的 App.onLaunch 函数中,您可以获得 BASEURL 并使用它来加载您的所有资产:

    App.onLaunch = function(options) {
      var BASEURL = options.BASEURL;
      // etc.
    }
    

    您还可以考虑使用不同的方法来加载模板。考虑使用 DOMParser 来解析 XML 字符串。这样您就可以编写具有“真实”内容的多行模板字符串,例如

    getDocument(options) {
    let parser = new DOMParser();
    let templateString = `<?xml version="1.0" encoding="UTF-8" ?>
        <document>
           <alertTemplate>
              <description>${options.translate.errorRandomErrorAlertText}</description>
              <button>
                 <text>${options.translate.utilOk}/text>
              </button>
           </alertTemplate>
        </document>`;
    return parser.parseFromString(templateString, "application/xml");
    }
    

    我写了一个generator 专门针对使用 es6 的 Apple TVML 应用程序,它仍处于开发初期,但可能会帮助您入门。

    【讨论】:

      【解决方案2】:

      尝试替换它

      var templateURL = 'niclasblog.com/appletv/main.tvml';
      

      具有完全限定的 URI

      var templateURL = 'https://niclasblog.com/appletv/main.tvml';
      

      还有

      templateXHR.responseType = "document"; 
      

      不需要,因为这似乎是默认行为。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-14
        • 2013-12-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多