How to embed a Youtube video into my app?

Use YouTube-Player-iOS-Helper:

Step 1: add pod "youtube-ios-player-helper", "~> x.y.z" to your Podfile, replace "x.y.z" with the latest version. and run pod install.

Step 2: Implement this code:

import UIKit
import youtube_ios_player_helper
 
class ViewController: UIViewController, YTPlayerViewDelegate {
    @IBOutlet weak var playerView: YTPlayerView!

    override func viewDidLoad() {
        super.viewDidLoad()
        playerView.delegate = self

        let playerVars = ["playsinline": 1] // 0: will play video in fullscreen
        self.playerView.loadWithVideoId("youtubeId", playerVars: playerVars)
    }     
}

Note: Set the rel key to 0 in your playerVars dictionary if you don't want to show related video:

let playerVars = ["playsinline":1, "rel" : 0 ]


Swift 3/4

You can play youtube video in AVPlayer with the help of XCDYouTubeKit.

Add

pod 'XCDYouTubeKit'

into your project and write a code as below

func playVideo() {

        let playerViewController = AVPlayerViewController()
        self.present(playerViewController, animated: true, completion: nil)

        XCDYouTubeClient.default().getVideoWithIdentifier("KHIJmehK5OA") { (video: XCDYouTubeVideo?, error: Error?) in
            if let streamURL = video?.streamURLs[XCDYouTubeVideoQuality.HD720.rawValue] {
                playerViewController.player = AVPlayer(url: streamURL)
            } else {
                self.dismiss(animated: true, completion: nil)
            }
        }
    }

you can change the quality of video by replacing XCDYouTubeVideoQuality.HD720.rawValue with medium360 or with Small240


Xcode 8.2 • Swift 3.0.2

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var wv: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        loadYoutube(videoID: "oCm_lnoVf08")
    }
    func loadYoutube(videoID:String) {
        guard
            let youtubeURL = URL(string: "https://www.youtube.com/embed/\(videoID)")
            else { return }
        wv.loadRequest( URLRequest(url: youtubeURL) )
    }
}

Xcode 7.3.1 • Swift 2.x

import UIKit

class ViewController: UIViewController {
    // create an outlet for your webview 
    @IBOutlet weak var wv: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // load your you tube video ID
        loadYoutube(videoID: "oCm_lnoVf08")
    }
    func loadYoutube(videoID videoID:String) {
        // create a custom youtubeURL with the video ID
        guard
            let youtubeURL = NSURL(string: "https://www.youtube.com/embed/\(videoID)")
            else { return }
        // load your web request
        wv.loadRequest( NSURLRequest(URL: youtubeURL) )
    }
}