Search code examples
javascripturltvostvml

tvOS not loading external TVML file


While creating a new Client-Server tvOS App, tvOS will not get the data from my external tvml file. This is the Error: ITML <Error>: Failed to load launch URL with error: (null)

This is the main.js code

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');
}

And I have attached the main.tvml file as well

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

That code is directly from the Apple Documentation, so I do not know why it is not working.


Solution

  • In your App.onLaunch function you could get the BASEURL and use this for loading all your assets:

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

    You can also consider a different approach to loading templates. Consider using a DOMParser to parse an XML String. This way you can write Multiline template strings with "real" content e.g.

    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");
    }
    

    I've written a generator for specfically for Apple TVML apps using es6, its still early in its development, but might help you get started.