可可:Youtube视频在WKWebview中不支持全屏 [英] Cocoa: Youtube video does not support full screen in WKWebview
问题描述
我正在使用Xcode制作网络浏览器,我使用浏览器播放,然后取消了它的播放功能,因此无法播放全屏youtube视频和嵌入的视频.我已经尝试过此代码...
I’m making a web browser using Xcode, I played with the browser then relized the it dosn’t play full screen youtube video’s and embeded videos. I have tried this code...
let webViewConfiguration = WKWebViewConfiguration()
webViewConfiguration.allowsInlineMediaPlayback = false
WKWebView(frame: .zero, configuration: webViewConfiguration)
但是它说 Wkwebview没有allowsInlineMediaPlayback的成员.
这是youtube所显示内容的图像
But it says Wkwebview has no member of allowsInlineMediaPlayback.
This is an image of what youtube shows
这是我的代码,以获取更多详细信息……
Here is my code for more detail…
import Cocoa
import WebKit
class ViewController: NSViewController, WKUIDelegate, NSSharingServicePickerDelegate, WKNavigationDelegate {
let kAlreadyBeenLaunched = "AlreadyBeenLaunched"
// MARK: - Outlets
@IBOutlet weak var messengerView: NSView!
@IBOutlet weak var googleBookMarkOut: NSButton!
@IBOutlet weak var googleSearchField: NSSearchField!
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var sideBar: NSBox!
@IBOutlet weak var searchBar: NSBox!
@IBOutlet weak var webBox: NSBox!
// MARK: - View Did Load
override func viewDidLoad() {
super.viewDidLoad()
self.webView.configuration.preferences.setValue(true, forKey: "developerExtrasEnabled")
self.webView.navigationDelegate = self
let myURL = URL(string: "https://www.google.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
//When user first opens app
if !UserDefaults.standard.bool(forKey: kAlreadyBeenLaunched) {
UserDefaults.standard.setValue(NSNumber(value: true), forKey: kAlreadyBeenLaunched)
//What users will see when they enter this app the first time
sideBar.borderColor = .clear
searchBar.borderColor = .clear
webBox.borderColor = .clear
}
}
override func viewDidAppear() {
self.webView.configuration.preferences.setValue(true, forKey: "developerExtrasEnabled")
}
// MARK: - Useless
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
// MARK: - Webview
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
webView.load(navigationAction.request)
}
return nil
}
// MARK: - Side Bar Buttons
@IBAction func homegoole(_ sender: Any) {
let myURL = URL(string: "https://www.google.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
self.view.window?.tab.title = webView.title!
}
@IBAction func shareButton(_ sender: Any) {
let ShareURL = webView.url
let sharingpicker = NSSharingServicePicker(items: [ShareURL as Any])
sharingpicker.delegate = self
sharingpicker.show(relativeTo: NSZeroRect, of: sender as! NSView, preferredEdge: .minY)
}
@IBAction func googleBookMark(_ sender: Any) {
let helealo = URL(string:"https://www.google.com/bookmarks/")
let helleago1 = URLRequest(url: helealo!)
webView.load(helleago1)
}
@IBAction func refreshButton(_ sender: Any) {
webView.reload()
}
// MARK: - Colour Buttons
@IBAction func themePink(_ sender: Any) {
sideBar.borderColor = .systemPink
searchBar.borderColor = .systemPink
webBox.borderColor = .systemPink
}
@IBAction func themeRed(_ sender: Any) {
sideBar.borderColor = .systemRed
searchBar.borderColor = .systemRed
webBox.borderColor = .systemRed
}
@IBAction func themeOrange(_ sender: Any) {
sideBar.borderColor = .systemOrange
searchBar.borderColor = .systemOrange
webBox.borderColor = .systemOrange
}
@IBAction func themeYellow(_ sender: Any) {
sideBar.borderColor = .systemYellow
searchBar.borderColor = .systemYellow
webBox.borderColor = .systemYellow
}
@IBAction func themeGrey(_ sender: Any) {
sideBar.borderColor = .systemGray
searchBar.borderColor = .systemGray
webBox.borderColor = .systemGray
}
@IBAction func themePurple(_ sender: Any) {
sideBar.borderColor = .systemPurple
searchBar.borderColor = .systemPurple
webBox.borderColor = .systemPurple
}
@IBAction func themeBlue(_ sender: Any) {
sideBar.borderColor = .blue
searchBar.borderColor = .blue
webBox.borderColor = .blue
}
@IBAction func lightBlue(_ sender: Any) {sideBar.borderColor = .systemBlue
searchBar.borderColor = .systemBlue
webBox.borderColor = .systemBlue
}
@IBAction func themeTorquise(_ sender: Any) {
sideBar.borderColor = .init(red: 44/255, green: 192/255.0, blue: 210/255, alpha: 1)
searchBar.borderColor = .init(red: 44/255, green: 192/255.0, blue: 210/255, alpha: 1)
webBox.borderColor = .init(red: 44/255, green: 192/255.0, blue: 210/255, alpha: 1)
}
@IBAction func themeGreen(_ sender: Any) {
sideBar.borderColor = .systemGreen
searchBar.borderColor = .systemGreen
webBox.borderColor = .systemGreen
}
// MARK: - Side Bar Button
@IBAction func showTitle(_ sender: Any) {
self.view.window?.tab.title = webView.title!
self.view.window?.title = webView.title!
}
// MARK: - Google/URL searche
@IBAction func urlSearche(_ sender: Any) {
let urlStr = googleSearchField.stringValue
if urlStr.hasPrefix("https://") {
let url = NSURL(string: urlStr)
let req = NSURLRequest(url:url! as URL)
self.webView!.load(req as URLRequest)
} else if urlStr.hasPrefix("http://") {
let url = NSURL(string: urlStr)
let req = NSURLRequest(url:url! as URL)
self.webView!.load(req as URLRequest)
}
else if urlStr.hasPrefix("www."){
let urlWithOUTHTPPS = "https://" + googleSearchField.stringValue
let url = NSURL(string: urlWithOUTHTPPS)
let req = NSURLRequest(url:url! as URL)
self.webView!.load(req as URLRequest)
} else if urlStr.hasSuffix(".com") {
let urlWithOUTHTPPS = "https://www." + googleSearchField.stringValue
let url = NSURL(string: urlWithOUTHTPPS)
let req = NSURLRequest(url:url! as URL)
self.webView!.load(req as URLRequest)
} else if urlStr.hasSuffix(".ca") {
let urlWithOUTHTPPS = "https://www." + googleSearchField.stringValue
let url = NSURL(string: urlWithOUTHTPPS)
let req = NSURLRequest(url:url! as URL)
self.webView!.load(req as URLRequest)
} else if urlStr.hasSuffix(".org") {
let urlWithOUTHTPPS = "https://www." + googleSearchField.stringValue
let url = NSURL(string: urlWithOUTHTPPS)
let req = NSURLRequest(url:url! as URL)
self.webView!.load(req as URLRequest)
} else if urlStr.hasSuffix(".net") {
let urlWithOUTHTPPS = "https://www." + googleSearchField.stringValue
let url = NSURL(string: urlWithOUTHTPPS)
let req = NSURLRequest(url:url! as URL)
self.webView!.load(req as URLRequest)
} else if urlStr.hasSuffix(".int") {
let urlWithOUTHTPPS = "https://www." + googleSearchField.stringValue
let url = NSURL(string: urlWithOUTHTPPS)
let req = NSURLRequest(url:url! as URL)
self.webView!.load(req as URLRequest)
} else if urlStr.hasSuffix(".edu") {
let urlWithOUTHTPPS = "https://www." + googleSearchField.stringValue
let url = NSURL(string: urlWithOUTHTPPS)
let req = NSURLRequest(url:url! as URL)
self.webView!.load(req as URLRequest)
} else if urlStr.hasSuffix(".gov") {
let urlWithOUTHTPPS = "https://www." + googleSearchField.stringValue
let url = NSURL(string: urlWithOUTHTPPS)
let req = NSURLRequest(url:url! as URL)
self.webView!.load(req as URLRequest)
} else if urlStr.hasSuffix(".mil") {
let urlWithOUTHTPPS = "https://www." + googleSearchField.stringValue
let url = NSURL(string: urlWithOUTHTPPS)
let req = NSURLRequest(url:url! as URL)
self.webView!.load(req as URLRequest)
} else {
let textToSearch = googleSearchField.stringValue
let allowedCharacters = NSCharacterSet.urlFragmentAllowed
guard let encodedSearchString = textToSearch.addingPercentEncoding(withAllowedCharacters: allowedCharacters) else { return }
let queryString = "https://www.google.de/search?q=\(encodedSearchString)"
guard let queryURL = URL(string: queryString) else { return }
let myRequest = URLRequest(url:queryURL)
webView.load(myRequest)
}
}
@IBAction func googleBookMarks(_ sender: Any) {
let myURL = URL(string: "https://www.google.com/bookmarks")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
override func keyDown(with event: NSEvent) {
if event.keyCode == 35 {
}
}
@IBAction func messengerButton(_ sender: Any) {
messengerView.isHidden = false
}
}
有什么办法可以在Wkwebview中制作全屏视频?
Is there any way I can make full screen videos in Wkwebview?
推荐答案
当您在 WKWebViewConfiguration上调用属性时,很奇怪,您收到了
对象. Wkwebview没有allowInlineMediaPlayback
问题的成员
Well it's weird that you got the Wkwebview has no member of allowsInlineMediaPlayback
issue when you are calling the property on a WKWebViewConfiguration
object.
您的第一个代码示例是执行此操作的好方法.也许您收到的错误来自先前的版本?
Your first code sample was the good way to do so. Maybe the error you got came from a previous build ?
经过快速测试后,我确认这是您的操作方式.
And after a quick test I confirm this is the way you do it.
这篇关于可可:Youtube视频在WKWebview中不支持全屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!