1

在创建新的客户端-服务器 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 文档,所以我不知道为什么它不起作用。

4

2 回答 2

1

尝试更换这个

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

具有完全限定的 URI

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

templateXHR.responseType = "document"; 

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

于 2015-11-02T16:14:15.940 回答
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");
}

我已经为使用 es6 的 Apple TVML 应用程序编写了一个生成器,它仍处于开发初期,但可能会帮助您入门。

于 2016-02-09T15:07:17.540 回答