Cocoa: Youtube video does not support full screen in WKWebview

泪湿孤枕 提交于 2021-01-28 08:01:58

问题


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)

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

    }
}

Is there any way I can make full screen videos in Wkwebview?

来源:https://stackoverflow.com/questions/61977288/cocoa-youtube-video-does-not-support-full-screen-in-wkwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!