Web app in tvOS
Asked Answered
H

6

27

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.

Haiduk answered 10/9, 2015 at 13:42 Comment(0)
P
47

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.

Packton answered 22/9, 2015 at 10:53 Comment(17)
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 :)Haiduk
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 UIWebViewPackton
Code like this will most likely get your app rejected.Lapstrake
Yeah, I already told that, the webview is there, but just to render the TVML, html websites are not usable and will be rejectedPackton
in Swift , i can't initWithFrame webviewClass. Please help me a ideaPaganize
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 TVJSPackton
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.Haiduk
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 elementsPackton
Hi @Packton 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?Shrievalty
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.Packton
@Packton thanks mate! Appreciate the answer! Could you elaborate a little more on the different approaches to handling this issue?Shrievalty
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 appsPackton
Nice! Thanks a lot! Nice hack! Hopefully work arounds like this will stay around for a whileShrievalty
@Packton Your code works great but the webview is off center so its clipped and not properly stretched. Im using the AppleTV emulator. Any ideas?Pray
@Packton Any idea how i would be able to add a UIWebViewConfiguration with this method? Thanks!Pray
More specifically I am looking to set mediaPlaybackRequiresUserAction = falsePray
How I hate the product policy of this former great company. Running towards the closed paywall web.Irradiant
D
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.

Dooryard answered 10/9, 2015 at 13:51 Comment(0)
O
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.

Overburdensome answered 11/12, 2015 at 14:9 Comment(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 allPackton
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!Overburdensome
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?Extrude
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 loadingRayerayfield
Do you know if the same can be done for using WKWebView? Some players are not working well in UIWebViewShornick
S
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 :(

Skilled answered 10/9, 2015 at 14:9 Comment(0)
P
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.

Pericline answered 19/9, 2015 at 4:16 Comment(0)
I
-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.

Irs answered 30/1, 2016 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.