Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

xcode - WKWebView Mac Browser Enable fullscreen html5?

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.

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).


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?

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

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).

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

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

And simply call it:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...