WKWebView Mac浏览器是否启用全屏html5? [英] WKWebView Mac Browser Enable fullscreen html5?

查看:423
本文介绍了WKWebView Mac浏览器是否启用全屏html5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Swift的最新版本和xcode 8.3.3创建了一个简单的浏览器. 我希望能够在有html5视频(例如youtube上)的情况下以全屏方式输入. 我现在在YouTube上看到全屏不可用". 旧的WebView出现同样的问题...在iOS上可以正常工作.

I made a simple browser with Swift last version and xcode 8.3.3. I want to be able to enter in fullscreen when there is an html5 video (like on youtube). I get "full screen is unavailable" on youtube right now. Same problem with the old WebView... on iOS it work.

编辑.也许只是不可能.我尝试查看JavaFX WebView和WPF WebBrowser,它们具有相同的局限性.实际上,只有一个人能够通过在WPF WebBrowser上全屏显示youtube视频,但只能通过创建完整的html页面来实现: 在WebBrowser控件中全屏播放youtube

EDIT. Maybe it's just not possible. I tried to look at JavaFX WebView and WPF WebBrowser and they have the same limitation. Actually one guy was able to allow full screen for a youtube video on WPF WebBrowser but only by creating a full html page: Playing youtube in full screen in WebBrowser control

当然,我不能为页面中的每个视频创建一个网页(至少我不知道如何,如果您知道的话,请告诉我).

And of course I cannot create a webpage for every video in a page (at least I don't know how, if you know please tell me).

原始消息: 我使用Swift的最新版本和xcode 8.3.3创建了一个简单的浏览器. 一切正常,但是我无法像使用旧版WebView那样激活插件. 由于我使用的是Mac,因此我应该能够激活插件(我知道在iOS上是不可能的),我错了吗?

ORIGINAL MESSAGE: I made a simple browser with Swift last version and xcode 8.3.3. Everything is working but I'm not able to activate plugins like I can do with the old WebView. Since I'm on a mac I should be able to activate plugins (I understand that it's not possible on iOS) am I wrong?

(在旧的WebView中,我也遇到了同样的问题)无法激活html5视频的全屏显示(至少我不知道如何操作).

Also (and here I got the same problem in the old WebView) there is no way to activate fullscreen for html5 videos (at least I don't know how).

@IBOutlet weak var webView: WKWebView!
let urlString = "http://myurl.com/"
self.webView.load(NSURLRequest(url: NSURL(string: urlString)! as URL) as URLRequest!)
self.webView.configuration.preferences.plugInsEnabled = true

这是使基本浏览器正常工作的真正基本代码.但是在WKWebView的Interface Builder中没有启用插件的选项,我真的不知道如何为html5视频(如youtube)提供全屏显示.

This is the really basic code to get a basic browser working. But there is no option to enable plugin in the Interface Builder for WKWebView and I really don't know how to allow fullscreen for html5 videos (like youtube).

编辑.好的,我终于找到了插件部分的答案: self.webView.configuration.preferences.plugInsEnabled = true

EDIT. Ok I finally found an answer for the plugin part: self.webView.configuration.preferences.plugInsEnabled = true

确实很容易,但是很难理解在哪里可以找到它,我必须去这里: https://developer.apple.com/documentation/webkit/webpreferences 猜一猜...

really easy but it was difficult to understand where to find it I had to go here: https://developer.apple.com/documentation/webkit/webpreferences and take a guess...

推荐答案

要允许WKWebView使用全屏HTML,您需要访问私有API(请参阅

To allow WKWebView to use fullscreen HTML you need to access private API's (see https://github.com/WebKit/webkit/blob/master/Source/WebKit/UIProcess/API/Cocoa/WKPreferences.mm#L232-L240) in WKPreferences. Since you're using Swift in your bridging header add:

#import <WebKit/WebKit.h>

@interface WKPreferences ()
-(void)_setFullScreenEnabled:(BOOL)fullScreenEnabled;
@end

简单地称呼它:

let webView = WKWebView(frame: view.frame)
webView.configuration.preferences._setFullScreenEnabled(true)

如果您在退出全屏模式后发现网页视图的大小发生奇怪的变化,我发现这可以为我解决问题:

If you notice strange resizing of the web view once you exit fullscreen I found that this fixes the issue for me:

webView.autoresizingMask = [.width, .height]
view.addSubview(webView)

webView.translatesAutoresizingMaskIntoConstraints = false
webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true

注意:由于您正在访问私有API,因此在Mac App Store中将被拒绝.

Note: Since you're accessing private API this would be rejected on the Mac App Store.

这篇关于WKWebView Mac浏览器是否启用全屏html5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆