25

Apple has released it's SDK for apple tv yesterday.

Most apps for TVs are web based (html, javascript and css) and I would like to port an existing tv web app to the tvOS.

The SDK in Xcode shows no Webview implementation like in iOS for creating apps based in html, javascript and css, however, according to the docs, it is possible to use javascript using tvjs framework, but instead of html, apple has it's own markup language for TVs called TVML.

My question is: Is it possible to port an existing web app to tvOS (how?), or does it need to be reimplemented from scratch?

Thanks for your help.

6 Answers 6

46

EDIT: Before people keeps downvoting, I'm going to make clear that the UIWebView is present on the apple TV, but it's PROHIBITED to use it, so, the only real way of loading web apps on an apple TV is creating them with TVML and TVJS

If you want to use an UIWebView (as proof of concept) you can do this:

Objective-c

Class webviewClass = NSClassFromString(@"UIWebView");
id webview = [[webviewClass alloc] initWithFrame:self.view.frame];
NSURL * url = [NSURL URLWithString:@"https://www.google.com"];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
[self.view addSubview:webview];

swift

let webViewClass : AnyObject.Type = NSClassFromString("UIWebView")!
let webViewObject : NSObject.Type = webViewClass as! NSObject.Type
let webview: AnyObject = webViewObject.init()
let url = NSURL(string: "https://www.google.com")
let request = NSURLRequest(URL: url!)
webview.loadRequest(request)
let uiview = webview as! UIView
uiview.frame = CGRectMake(0, 0, view.frame.width, view.frame.height)
view.addSubview(uiview)

MAGIC!!! The UIWebView is there! But you can't iteract with it, just show web pages or web content.

UPDATE! I've found that there is a lot of tvOS browsers on github based on https://github.com/steventroughtonsmith/tvOSBrowser, but most of them require tweaking Availability.h on Xcode app. I've found a fork that uses my approach, so it doesn't require tweaking Availability.h https://github.com/FabioSpacagna/tvOSBrowser They add basic support for navigating, like scroll and a cursor

I don't think apple will approve apps using UIWebView as it's marked as prohibited, you'll have to learn TVML and TVJS instead.

17
  • 1
    Just the remote is not prepared for it, there is no way of navigating through the content with swipes on the remote, but I guess it will be a matter of adding gesture recognisers and forcing events on javascript, thanks :) Sep 23, 2015 at 14:30
  • Yeah, it needs a lot of work to make it usable, gesture recognizers, delegates, not sure if it's worth the effort if apple will probably reject the apps using UIWebView Sep 23, 2015 at 14:58
  • Code like this will most likely get your app rejected. Oct 5, 2015 at 19:22
  • Yeah, I already told that, the webview is there, but just to render the TVML, html websites are not usable and will be rejected Oct 5, 2015 at 20:48
  • in Swift , i can't initWithFrame webviewClass. Please help me a idea Nov 23, 2015 at 3:53
11

According to the API diffs, UIWebView, etc are not part of tvOS. So you'll have to re-implement the app using TVML (TV Markup Language) using Apple's templates. Or you can re-implement using UIKit.

6

Here's the Swift version of jcesarmobile's solution.

    let webViewClass : AnyObject.Type = NSClassFromString("UIWebView")!
    let webViewObject : NSObject.Type = webViewClass as! NSObject.Type
    let webview: AnyObject = webViewObject.init()
    let url = NSURL(string: "https://www.google.com")
    let request = NSURLRequest(URL: url!)
    webview.loadRequest(request)
    let uiview = webview as! UIView
    uiview.frame = CGRectMake(0, 0, webDashboardView.frame.width, webDashboardView.frame.height)
    webDashboardView.addSubview(uiview)

I'm building an enterprise app that doesn't have to go through the App Store, so this solution works for me.

5
  • did you make any progress on the website iteraction? with this code you just load a website, but you can't really navigate or do anything at all Dec 11, 2015 at 18:50
  • No progress on website interaction. Since I'm just showing static data in my app, it's not important for my needs. But, would be curious if anyone got it to work! Dec 12, 2015 at 19:47
  • The above code works well. I have a need though to set: webview.mediaPlaybackRequiresUserInteraction to false (it is true by default). I change let webview, to var webview .. and it says that "cannot assign to property. webview is immutable" Any suggestions?
    – Jim
    Apr 12, 2017 at 11:06
  • I know this is a very old question. But I used this implementation into an enterprise app.. the webvivew was working fine no problem until a few days later when it was displaying just a white blank screen. This issue is not able to be reproduced on the simulator. It works no problem on simulator and worked on a deployed apple tv for several days before the webivew started displaying a blank screen... as if its not loading Oct 15, 2019 at 19:21
  • Do you know if the same can be done for using WKWebView? Some players are not working well in UIWebView Jun 22, 2020 at 13:31
4

UIWebView is part of the tvOS, although the documentation looks a bit limited right now (see here). You can also find the .h file here: /Applications/Xcode-beta.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIWebView.h

WebKit framework doesnt seem to be included.

Update:

Found in UIWebView.h: NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIWebView : UIView <NSCoding, UIScrollViewDelegate>

Taking this into account it might be not available in tvOS :(

2

Unfortunately, UIWebView is not a supported class in tvOS. Niether is WKWebView. You can see a full list of the APIs supported and removed in iOS 9 and tvOS here. If you scroll down to the bottom of the first section you'll see WebKit says removed.

0
-1

If you are looking to port your existing application to tvOS, you might want to consider atvjs framework. It would give you the familiar experience of developing any front-end heavy application. You may still need to modify your existing web app to suite the framework, however it would be a much less painful process than using the sample architecture provided by Apple.

To get started, refer to https://github.com/emadalam/tvml-catalog-using-atvjs which is a port of the original sample code, re-written using atvjs framework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.