Search code examples
swiftswift2tvostvmltvapplicationcontroller

tvOS - Detect when TVML is dismissed from my UIViewController


I have a view controller for my app that calls another view controller modally to cover the screen with a blur effect. Inside this other view controller, I'm displaying a TVApplicationController to display TVML content with transparent background on top of this blurred view.

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: nil, delegate: self)

appController?.navigationController.modalPresentationStyle = UIModalPresentationStyle.OverFullScreen

self.presentViewController((appController?.navigationController)!, animated: true, completion: nil)

What I want to do is, when I press the MENU button, to make the TVML content go away and to dismiss my modal blur view controller. The problem is that I'm not being able to detect the "dismissal" of the TVML content so I can close my modal view controller.

I tried to use the TVApplicationControllerDelegate to receive the messages that might come while using it but nothing helped.


Solution

  • I just found a workaround for this. I created a small class like this:

    import UIKit
    
    class HiddenView: UIView {
    
        override func canBecomeFocused() -> Bool {
            return true;
        }
    }
    

    Then, what I did is to create an instance of this HiddenView on the ViewDidLoad of the blurred view controller and add it to the view controllers's view.

    let hiddenView = HiddenView(frame: CGRectMake(0,0,10,10))
    self.view.addSubview(hiddenView)
    // it won't appear on the screen since it has no color/text/etc
    

    Now, when I press the MENU button on the remote, when the TVML content is dismissed, the delegate method didUpdateFocusInContext on my blurred modal view controller is called, so I can dismiss it like this:

    override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    

    If anyone knows a better way to handle this than having to do this workaround, it would be nice to know.