可可:Youtube视频在WKWebview中不支持全屏

我正在使用Xcode制作网络浏览器,然后使用浏览器播放,然后意识到它无法播放全屏youtube视频和嵌入的视频。我已经尝试过此代码...

let webViewConfiguration = WKWebViewConfiguration()
webViewConfiguration.allowsInlineMediaPlayback = false
WKWebView(frame: .zero, configuration: webViewConfiguration)

But it says Wkwebview has no member of allowsInlineMediaPlayback. This is an image of what youtube shows

enter image description here

这是我的代码,详细信息……

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中制作全屏视频?

评论