javascript tvOS 中的网络应用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32503582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:23:15  来源:igfitidea点击:

Web app in tvOS

javascripthtmlweb-applicationsapple-tvtvos

提问by Marcio Oliveira

Apple has released it's SDK for apple tvyesterday.

苹果昨天发布了它的苹果电视 SDK

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.

大多数电视应用程序都是基于网络的(html、javascript 和 css),我想将现有的电视网络应用程序移植到 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.

Xcode 中的 SDK 没有像 iOS 中那样显示用于创建基于 html、javascript 和 css 的应用程序的 Webview 实现,但是,根据文档,可以使用tvjs 框架使用 javascript ,但苹果有自己的标记语言而不是 html用于称为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?

我的问题是:是否可以将现有的 Web 应用程序移植到 tvOS(如何?),还是需要从头开始重新实现?

Thanks for your help.

谢谢你的帮助。

回答by jcesarmobile

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

编辑:在人们继续投票之前,我将明确说明 Apple TV 上存在 UIWebView,但禁止使用它,因此,在 Apple TV 上加载 Web 应用程序的唯一真正方法是使用 TVML 创建它们和TVJS

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

如果你想使用 UIWebView(作为概念证明),你可以这样做:

Objective-c

目标-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 UIWebViewis there! But you can't iteract with it, just show web pages or web content.

魔法!!!在UIWebView那里!但是你不能用它进行迭代,只能显示网页或网页内容。

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/tvOSBrowserThey add basic support for navigating, like scroll and a cursor

更新!我发现 github 上有很多基于https://github.com/steventroughtonsmith/tvOSBrowser的 tvOS 浏览器,但其中大部分都需要在 Xcode 应用程序上调整 Availability.h。我找到了一个使用我的方法的叉子,所以它不需要调整 Availability.h https://github.com/FabioSpacagna/tvOSBrowser他们添加了对导航的基本支持,比如滚动和光标

I don't think apple will approve apps using UIWebViewas it's marked as prohibited, you'll have to learn TVMLand TVJSinstead.

我不认为苹果会批准应用程序,UIWebView因为它被标记为禁止使用,你必须学习TVMLTVJS而不是。

回答by Jess Bowers

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.

根据API 差异,UIWebView 等不是 tvOS 的一部分。因此,您必须使用 Apple 的模板,使用 TVML(电视标记语言)重新实现该应用程序。或者您可以使用 UIKit 重新实现。

回答by Justin Domnitz

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

这是 jcesarmobile 解决方案的 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, 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.

我正在构建一个不需要通过 App Store 的企业应用程序,所以这个解决方案对我有用。

回答by Maciek Czarnik

UIWebViewis 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

UIWebView是 tvOS 的一部分,尽管目前文档看起来有点有限(请参阅此处)。您还可以在此处找到 .h 文件:/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.

似乎没有包含 WebKit 框架。

Update:

更新:

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

在 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:(

考虑到这一点,它可能在 tvOS 中不可用:(

回答by Viper

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.

不幸的是,UIWebView 不是 tvOS 支持的类。Niether 是 WKWebView。您可以在此处查看iOS 9 和 tvOS 中支持和删除的 API 的完整列表。如果您向下滚动到第一部分的底部,您会看到 WebKit 说已删除。

回答by eMAD

If you are looking to port your existing application to tvOS, you might want to consider atvjsframework. 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.

如果您希望将现有应用程序移植到 tvOS,您可能需要考虑atvjs框架。它将为您提供开发任何前端繁重应用程序的熟悉体验。您可能仍需要修改现有的 Web 应用程序以适应该框架,但与使用 Apple 提供的示例架构相比,这将是一个痛苦得多的过程。

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

要开始使用,请参阅https://github.com/emadalam/tvml-catalog-using-atvjs,它是原始示例代码的端口,使用atvjs框架重新编写。