Search code examples
tvos

tvOS topshelf not launching application


I've set up top shelf for a sample tvOS application, but clicking on an topshelf item doesn't launch the application. I've set up the display URL, though I'm not sure if I'm doing it right...any one know exactly how to do this?


Solution

  • You probably miss the URL scheme in your .plist file.

    My info.plist URL scheme:

    info.plist URL scheme

    My displayURL of the top shelf item:

    var displayURL: NSURL {
        let components = NSURLComponents()
        components.scheme = "openApplication"
        components.path = "LiveStreamsViewController"
        components.queryItems = [NSURLQueryItem(name: "channelID", value: String(self.id))]
    
        return components.URL!
    }
    

    With this the application will open if the user clicked on an item in the top shelf. In appDelegate you can catch your URL and do something else with it:

    func application(app: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool {
        // When the user clicks a Top Shelf item, the application will be asked to open the associated URL.
        if let viewController = self.window?.rootViewController?.storyboard?.instantiateViewControllerWithIdentifier("liveStream") as? LiveStreamsViewController {
            if let queryItemValue = NSURLComponents(URL: url, resolvingAgainstBaseURL: false)?.queryItems?.first?.value, let channelID = Int(queryItemValue) {
                viewController.pageIndex = channelID
                self.window?.rootViewController?.presentViewController(viewController, animated: true, completion: nil)
            }
        }
    
        return true
    }
    

    In this example, I open another view controller instead of the main view controller.