WKWebView Mac 浏览器启用全屏 html5?

Lie*_*yer 4 macos xcode fullscreen html5-video wkwebview

我用 Swift 最新版本和 xcode 8.3.3 制作了一个简单的浏览器。当有 html5 视频(如在 youtube 上)时,我希望能够全屏输入。我现在在 youtube 上看到“全屏不可用”。旧的 WebView 有同样的问题......在 iOS 上它可以工作。

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

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


原始信息:我使用 Swift 最新版本和 xcode 8.3.3 制作了一个简单的浏览器。一切正常,但我无法像使用旧的 WebView 那样激活插件。因为我使用的是 mac,所以我应该能够激活插件(我知道在 iOS 上是不可能的)我错了吗?

另外(在这里我在旧的 WebView 中遇到了同样的问题)没有办法为 html5 视频激活全屏(至少我不知道如何)。

@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
Run Code Online (Sandbox Code Playgroud)

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

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

真的很容易,但很难理解在哪里可以找到它我不得不去这里:https : //developer.apple.com/documentation/webkit/webpreferences 并猜测......

Son*_*ohn 5

为了让WKWebView使用你需要访问的私有API的全屏HTML(见https://github.com/WebKit/webkit/blob/master/Source/WebKit/UIProcess/API/Cocoa/WKPreferences.mm#L232-L240)的WKPreferences。由于您在桥接头中使用 Swift,因此请添加:

#import <WebKit/WebKit.h>

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

Run Code Online (Sandbox Code Playgroud)

并简单地调用它:

let webView = WKWebView(frame: view.frame)
webView.configuration.preferences._setFullScreenEnabled(true)
Run Code Online (Sandbox Code Playgroud)

如果您在退出全屏后发现 Web 视图的大小发生奇怪的调整,我发现这为我解决了这个问题:

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
Run Code Online (Sandbox Code Playgroud)

注意:由于您正在访问私有 API,这将在 Mac App Store 上被拒绝。


vau*_*all 5

您可以通过在配置中使用 KVO 更改属性来完成此操作,而不是使用私有 API。

使用 Swift 5:

let configuration = WKWebViewConfiguration()
configuration.preferences.setValue(true, forKey: "fullScreenEnabled")
let webView = WKWebView(frame: .zero, configuration: configuration)
Run Code Online (Sandbox Code Playgroud)

在 macOS 10.15 和 11 上测试