How can we play YouTube Videos now with iOS?

Viewed 3998

I'm taking a course online that shows how to play videos from a url. But it's always the bunny one. I've been looking how to play YouTube videos, been finding that they use to use UIWebView. Since it's been deprecated, how would I be able to play them now. This is how my code looks like.

import UIKit
import AVKit
import AVFoundation

class CourseDetailVC: UIViewController {
    @IBOutlet weak var descriptionTextView: UITextView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var authorButton: UIButton!
    @IBOutlet weak var backgroundImage: UIImageView!

    var course: Courses.Course?

    @IBAction func playURLVideo() {
        guard let videoURL = URL(string: course?.videoUrl ?? "") else { return }
        let player = AVPlayer(url: videoURL)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.present(playerViewController, animated: true) {
            playerViewController.player!.play()
        }
    }

I just have a button. When I press it I would like to play a YouTube video. Now this works with the bunny one. How would I be able to play this YouTube video.

Screenshot after button is press

enter image description here

Updated Code

 @IBAction func playButtonPress() {
    // Create Video player
    var mywkwebview: WKWebView?
    let mywkwebviewConfig = WKWebViewConfiguration()

    mywkwebviewConfig.allowsInlineMediaPlayback = true
    mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)

    let myURL = URL(string: "https://www.youtube.com/embed/JePnQ1gSagc?playsinline=1?autoplay=1")
    let youtubeRequest = URLRequest(url: myURL!)

    mywkwebview?.load(youtubeRequest)

    guard let webView = mywkwebview else { return }
    self.view.addSubview(webView)

}
1 Answers

Here's a solution to WKWebView

Since YouTube has functionality to load any video in fullscreen in a webview without any of the scrolling features using a URL in the format https://www.youtube.com/embed/<videoId>.

The given video above has an ID of (https://www.youtube.com/watch?v=1roy4o4tqQM) -- [1roy4o4tqQM]

mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)
let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
var youtubeRequest = URLRequest(url: myURL!)
mywkwebview.load(youtubeRequest)

Updated code:

@IBAction func playURLVideo() {

  var mywkwebview: WKWebView?
  let mywkwebviewConfig = WKWebViewConfiguration()

  mywkwebviewConfig.allowsInlineMediaPlayback = true
  mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)

  let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
  var youtubeRequest = URLRequest(url: myURL!)

  mywkwebview.load(youtubeRequest)
}

You need to add ?autoplay=1 to the end of the embed URL

Related