Search code examples
ioslocalhosttvostvml

tvOS - Cannot connect to localhost


I am testing some tvOS code and it works great on the Simulator however on the Dev Kit running OS beta 2 I am receiving the following error:

2015-09-27 11:10:29.797 RWDevCon[272:46267] SSSQLiteDatabase: Could not open database, resetting: [23, /var/mobile/Library/Cookies/com.apple.itunesstored.2.sqlitedb] _CFURLRequestCopyStorageSession deprecated 2015-09-27 11:10:29.810 RWDevCon[272:46267] ITML: Setting [1] for Ignore HTTP Cache 2015-09-27 11:10:34.886 RWDevCon[272:46267] ITML: Setting [3] log level 2015-09-27 11:10:34.887 RWDevCon[272:46267] ITML : Failed to load launch URL with error: Error Domain=NSURLErrorDomain Code=-1004 "Cannot connect to localhost" UserInfo={NSUnderlyingError=0x137d2dee0 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "Could not connect to the server." UserInfo={ _kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1, NSLocalizedDescription=Could not connect to the server.}}, NSErrorFailingURLStringKey=http://localhost:9001/js/application.js, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61, NSLocalizedDescription=Cannot connect to localhost}

Swift Code: class AppDelegate: UIResponder, UIApplicationDelegate, TVApplicationControllerDelegate { var window: UIWindow?

var appController: TVApplicationController? /
static let TVBaseURL = "http:/
static let TVBootURL = "\(AppDelegate.TVBaseURL)js/application.js" /

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {

    window = UIWindow(frame: UIScreen.mainScreen().bounds)

    /
    let appControllerContext = TVApplicationControllerContext()

    /
    guard let javaScriptURL = NSURL(string: AppDelegate.TVBootURL) else {
        fatalError("unable to create NSURL")
    }

    appControllerContext.javaScriptApplicationURL = javaScriptURL
    appControllerContext.launchOptions["BASEURL"] = AppDelegate.TVBaseURL

    /
    appController = TVApplicationController(context: appControllerContext, window: window, delegate: self)

    return true
}

}

**JavaScript/server code:**
App.onLaunch = function(options) {

// 1 var alert = createAlert("Welcome to tvOS Programming", "It will be Awesome!"); // leaving 2nd parameter with an empty string navigationDocument.presentModal(alert); }

// 2
var createAlert = function(title, description) {

var alertString = `<?xml version="1.0" encoding="UTF-8" ?>
<document>
<alertTemplate>
<title>${title}</title>
<description>${description}</description>
<button><text>OK</text></button>
</alertTemplate>
</document>`

var parser = new DOMParser();
var alertDoc = parser.parseFromString(alertString, "application/xml");

return alertDoc

}

Starting the Server:

python -m SimpleHTTPServer 9001

NB: Allow Arbitary Loads is set to YES


Solution

  • The devkit is another computer on your network, and you can't host the JSTVML server on the kit itself. You need to host it on an external server (in this case your development machine is that external server).

    Make sure your devkit and your development computer are on the same network.

    Set up your js code to access your computer by specifying the IP address.

    Ex: http://192.168.1.21:9001/js/application.js (pretending that 192.168.1.21 is the ip address of your development server and 9001 is the port.)

    You should be able to test this out by accessing it in your browser first (to make sure you're development computer is returning the .js file correctly).

    Once you know the 'server' is working, setup your javascript to access that remote location.

    Hope this helps.