Friday, April 3, 2015

Play an embedded Youtube video

1. Uncheck the 'Use Auto Layout' option in the file inspector.

2. Drag the web view to the storyboard.  

3. Control-drag the web view to ViewController.swift. Name it myWebView and obtain the code as below:


@IBOutlet weak var myWebView: UIWebView!

4. Complete the code as below:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var myWebView: UIWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let myURL : NSURL = NSURL(string: "
https://www.youtube.com/embed/vmMAI86Lub0")!
       //Note: use the "embed" address instead of the "watch" address.
        let myURLRequest : NSURLRequest = NSURLRequest(URL: myURL)
        self.myWebView.loadRequest(myURLRequest)
    }

No comments:

Post a Comment