WKWEbView Hide controls of native player in iOS

Viewed 830

I am reproducing a Youtube video in my iOS app. the problem I am facing is when the video plays it plays in the native video player for iOS.

Thats ok for me, but I want to disable the controls in the native player so the user cannot skip the video.

Is this possible?

This is my code :

func setUpUI() {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        webView.backgroundColor = .red 
        self.backgroundColor = .white
        webView.translatesAutoresizingMaskIntoConstraints = false
        self.contentView.addSubview(mainContainer)
        mainContainer.addSubview(lineSeparator)
        mainContainer.addSubview(bottomLineSeparator)
        mainContainer.addSubview(videoIcon)
        mainContainer.addSubview(watchItAll)
        mainContainer.addSubview(videoContainer)
        videoContainer.addSubview(videoTitleContainer)
        videoContainer.addSubview(webView)
        videoTitleContainer.addSubview(videoTitleLabel)

        lineSeparator.backgroundColor = .lightGreyBkgrnd
        lineSeparator.layer.borderColor = UIColor.lightGreyBkgrnd.cgColor
        lineSeparator.layer.borderWidth = 3

        videoContainer.layer.borderColor = UIColor.dimGray.cgColor
        videoContainer.layer.borderWidth = 2

        bottomLineSeparator.backgroundColor = .lightGreyBkgrnd
        bottomLineSeparator.layer.borderColor = UIColor.lightGreyBkgrnd.cgColor
        bottomLineSeparator.layer.borderWidth = 3

        self.videoTitleLabel.font = UIFont.preferredFont(forTextStyle: .headline)
        self.videoTitleLabel.textColor = UIColor.tuftsBlue

        videoPlayerSuperView.translatesAutoresizingMaskIntoConstraints = false
        self.watchItAll.font = UIFont.preferredFont(forTextStyle: .body)
        self.watchItAll.numberOfLines = 0

        //videoContainer.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(videoTapped)))

        let html = """
        <iframe width="100%" height="100%" src="https://www.youtube.com/embed/0oBx7Jg4m-o?autoplay=0&showinfo&controls=0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
        """
        webView.loadHTMLString(html, baseURL: nil)
    }
1 Answers

If you are using AVPlayerViewController to play the video, it has a property

open var showsPlaybackControlls: Bool

that you can set to NO to hide user controls.

Also, if you highlight the class and choose "Go to Definition", you will see other functions and properties of the class that you might want to decide to use.

Related