简体   繁体   中英

Reloading and debugging JavaScript in TVOS simulator

I have started looking at tutorials for making TVML/TVJS based apps for the new Apple TV, and I have two problems that makes the development process very tedious and impractical.

First thing I am having trouble understanding is how I am supposed to debug code that happens on startup of the application. I have connected the Safari debugger, and I do manage to hit some breakpoints, but only for code that is triggered by some user input. On startup I am loading an xml document from a remote location, and I will use this to dynamically generate the tvml template, but I cannot get the debugger to stop anywhere in the code that is running before the template is done rendering.

The other anti-productive problem I have is that I cannot seem to reload the JavaScript files in any other way than completely shutting down the application in the simulator (double-click the home button, and swipe the app away). This also makes the debugger quit, so I have to restart that one as well. This surely cannot be the way you are supposed to do continuous development and testing?

You can make the debugger stop at the first line when you choose the Auto Pause and Auto Show options from the Safari menu "Develop/Simulator".

You are correct about the exit issue. One thing you can also try is to run App.reload() from the Safari Debugger console. This also restarts the app, maybe in the future they can make it work so the debugger will not be gone. But at the moment this also does not solve the issue.

在此处输入图片说明

For manual debugger output (aka console.log() ), you could redirect the logging to the Xcode debugger.

(somewhere on the web) I found a way to actually do that, in short it looks like...

AppDelegate.Swift

func appController(appController: TVApplicationController, evaluateAppJavaScriptInContext jsContext: JSContext) {
    let jsInterface: cJsInterface = cJsInterface();
    jsContext.setObject(jsInterface, forKeyedSubscript: "swiftInterface")
}

App.js

// re-route console.log() to XCode debug window
var console = {
  log: function() {
    var message = '';
    for(var i = 0; i < arguments.length; i++) {
      message += arguments[i] + ' '
    };
    swiftInterface.log(message)
  }
};

JsInterface.Swift

@objc protocol jsInterfaceProtocol : JSExport {
    func log(message: String) -> Void
}
...
class cJsInterface: NSObject, jsInterfaceProtocol {
    func log(message: String) -> Void {
        print("JS: \(message)")
    }
}

Complete sources in github: https://github.com/iBaa/PlexConnectApp/tree/f512dfd9c1cb2fbfed2da43c4e3837435b0b22af

I don't have any solution for the dying debugger myself...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM