我正在构建一个 TVML/TVJS Apple TV 应用程序,但我需要能够通过播放器获得一些本机功能,因此我正在使用evaluateAppJavaScriptInContext
创建一个 JavaScript 函数,该函数将在调用时将自定义视图控制器推送到屏幕上。问题是它会在控制台中引起警告:
此应用程序正在从后台线程修改自动布局引擎,这可能导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。
代码如下所示:
import TVMLKit
import AVKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, TVApplicationControllerDelegate {
var window: UIWindow?
var appController: TVApplicationController?
var workoutViewController = WorkoutViewController()
static let TVBaseURL = "http://localhost:3000/"
static let TVBootURL = "\(AppDelegate.TVBaseURL)assets/tv.js"
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
// 1
let appControllerContext = TVApplicationControllerContext()
// 2
guard let javaScriptURL = NSURL(string: AppDelegate.TVBootURL) else {
fatalError("unable to create NSURL")
}
appControllerContext.javaScriptApplicationURL = javaScriptURL
appControllerContext.launchOptions["BASEURL"] = AppDelegate.TVBaseURL
// 3
appController = TVApplicationController(context: appControllerContext, window: window, delegate: self)
do {
guard let audioURL = NSURL(string: self.workoutViewController.audioURL) else {
fatalError("unable to create NSURL")
}
let audioPlayer = try AVAudioPlayer(contentsOfURL: audioURL)
if (audioPlayer.prepareToPlay()) {
audioPlayer.play()
}
} catch {
print("Error: \(error)")
}
return true
}
func appController(appController: TVApplicationController, evaluateAppJavaScriptInContext jsContext: JSContext) {
let presentWorkoutViewController : @convention(block) (String) -> Void = { (string : String) -> Void in
self.workoutViewController.jsonString = string
// dispatch_async(dispatch_get_main_queue(), {
self.appController?.navigationController.pushViewController(self.workoutViewController, animated: true)
// })
}
jsContext.setObject(unsafeBitCast(presentWorkoutViewController, AnyObject.self), forKeyedSubscript: "presentWorkoutViewController")
}
}
我尝试将其包装在 a 中dispatch_async
并修复了错误,但是当我尝试将本机视图控制器推回视图时,它仍然包含其旧内容,而不是我试图显示的新内容。
看起来像这样:
dispatch_async(dispatch_get_main_queue(), {
self.appController?.navigationController.pushViewController(self.workoutViewController, animated: true)
})
提前致谢!