1

I'm a beginner on TVOS.

I'd like to create an hybrid app on AppleTV using a native app and TVMLKIT. My native application is just a simple native app with buttons (using swift).

When we click on a button, I launch a a javascript app using TVLMKIT and TVJS.

My TVJS as uses the Player to display a video. When the video is over, I want to close the TVJS app and back to the native ViewController.

My problem is that when I back to native app, I loose the focus on my native View (the app is frozen).

native ViewController:

import UIKit
import TVMLKit

class ViewController: UIViewController, TVApplicationControllerDelegate {

var window: UIWindow?
var appController: TVApplicationController?
var appControllerContext = TVApplicationControllerContext();
static let TVBaseURL = "http://localhost:9001/"
static let TVBootURL = "\(ViewController.TVBaseURL)/client/js/application.js"

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBOutlet weak var label: UILabel!

@IBOutlet weak var viewAd: UIView!

@IBAction func clickOnlaunchAd(sender: AnyObject) {
    window = UIWindow(frame: UIScreen.mainScreen().bounds)

    guard let javaScriptURL = NSURL(string: ViewController.TVBootURL) else {
        fatalError("unable to create NSURL")
    }
    appControllerContext.javaScriptApplicationURL = javaScriptURL
    appControllerContext.launchOptions["BASEURL"] = ViewController.TVBaseURL

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

}
@IBAction func clickOnChangeText(sender: AnyObject) {
    label.text = "changed";
}

func appController(appController: TVApplicationController, didStopWithOptions options: [String : AnyObject]?) {
   self.setNeedsFocusUpdate()
   self.updateFocusIfNeeded()
}

func appController(appController: TVApplicationController, evaluateAppJavaScriptInContext jsContext: JSContext){
    let notifyEventToNative : @convention(block) (NSString!) -> Void = {
        (string : NSString!) -> Void in
            print("[log]: \(string)\n")
            self.appController?.stop()
    }
    jsContext.setObject(unsafeBitCast(notifyEventToNative, AnyObject.self), forKeyedSubscript: "notifyEventToNative")
}
}

Just before calling "notifyEventToNative" from my TVJS, I call "navigationDocument.clear();" to clear the TVML view.

I can see my native app but I can't interact with it.

Any ideas? Thanks.

SPKE30
  • 90
  • 4

1 Answers1

0

I also had the same problem. I was opened a TVML document from the UIViewController. And I also lost the focus. So, first of all I can advice you to override var called preferredFocusedView in your ViewController. In this method you can return reference to viewAd. But the better solution would be to wrap your ViewController into the TVML-item (with the TVMLKit framework). In that case I hope that you will have no problems with focus because you will use TVML during the whole application.

Roman Podymov
  • 4,168
  • 4
  • 30
  • 57