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.

Marcio Oliveira
  • 791
  • 1
  • 5
  • 18

6 Answers6

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.

jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
  • 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 :) – Marcio Oliveira Sep 23 '15 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 – jcesarmobile Sep 23 '15 at 14:58
  • Code like this will most likely get your app rejected. – LightningStryk Oct 05 '15 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 – jcesarmobile Oct 05 '15 at 20:48
  • in Swift , i can't initWithFrame webviewClass. Please help me a idea – Nguyen Hoan Nov 23 '15 at 03:53
  • Sorry, I don't know how to do it with swift, but anyway, don't waste your time with this, apple won't approve your app if you do this, the official answer here is to use TVML and TVJS – jcesarmobile Nov 23 '15 at 07:52
  • Well, you don't always need to develop for app store, you may need just an internal enterprise app as @Justin Domnitz mentioned with his swift example. – Marcio Oliveira Dec 11 '15 at 14:40
  • added swift code based on Justin answer. Anyway, with this you load the website, but it's still not usable, you can't type or click on website elements – jcesarmobile Dec 11 '15 at 18:56
  • Hi @jcesarmobile I am very unfamiliar in the area of Prohibited APIs (are they the same as Private APIs?) I've looked at the github repos and (sort of) understand the modifications to Availability.h to gain access to the UIWebView component. I don't follow what "your approach" is though and how it works, can you elaborate? Also, what do people think the chances this workaround will be removed in future TvOS versions? (I have no intention to release any apps I make) lastly, if it's prohibited, why do Apple release it in the SDK? – The Naughty Otter Aug 04 '16 at 07:31
  • Private and Prohibited aren't the same thing, but Apple will reject apps that use any of them. Private APIs can be used without modifying Availability.h. Prohibited APIs are APIs available for some OS but for sore reason Apple decides to block the use on other OS, in this case UIWebView is available on iOS, but prohibited on tvOS. They can't remove it from the SDK because it's the same for iOS and tvOS, so if they remove it won't be available for iOS, so they just prohibit it on tvOS. – jcesarmobile Aug 04 '16 at 07:53
  • @jcesarmobile thanks mate! Appreciate the answer! Could you elaborate a little more on the different approaches to handling this issue? – The Naughty Otter Aug 09 '16 at 12:29
  • As you can't use the UIWebView class on tvOS apps because it's prohibited, my approach is to get the class from the string with Class webviewClass = NSClassFromString(@"UIWebView");, but there are some thngs that you can't use following this approach, as delegates. In that case you can modify Availability.h os Xcode allows you to use UIWebView even on tvOS apps – jcesarmobile Aug 09 '16 at 13:33
  • Nice! Thanks a lot! Nice hack! Hopefully work arounds like this will stay around for a while – The Naughty Otter Aug 09 '16 at 14:25
  • @jcesarmobile Your code works great but the webview is off center so its clipped and not properly stretched. Im using the AppleTV emulator. Any ideas? – Sealer_05 Nov 03 '18 at 07:25
  • @jcesarmobile Any idea how i would be able to add a UIWebViewConfiguration with this method? Thanks! – Sealer_05 Jun 20 '19 at 03:03
  • More specifically I am looking to set mediaPlaybackRequiresUserAction = false – Sealer_05 Jun 20 '19 at 03:22
  • How I hate the product policy of this former great company. Running towards the closed paywall web. – digitaldonkey Oct 14 '20 at 12:43
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.

Jess Bowers
  • 2,846
  • 1
  • 22
  • 42
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.

Justin Domnitz
  • 3,217
  • 27
  • 34
  • 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 – jcesarmobile Dec 11 '15 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! – Justin Domnitz Dec 12 '15 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 '17 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 – Julian Silvestri Oct 15 '19 at 19:21
  • Do you know if the same can be done for using WKWebView? Some players are not working well in UIWebView – Amir Hajiha Jun 22 '20 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 :(

Maciek Czarnik
  • 5,950
  • 2
  • 37
  • 50
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.

Viper
  • 1,327
  • 2
  • 12
  • 33
-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.

eMAD
  • 473
  • 4
  • 6