Embedding Video into the App

Free iOS Development Tutorial

Learn how to embed a YouTube video in an iOS application with this comprehensive tutorial, which includes step-by-step instructions and detailed coding examples.

This exercise is excerpted from Noble Desktop’s past app development training materials and is compatible with iOS updates through 2021. To learn current skills in web development, check out our coding bootcamps in NYC and live online.

Topics covered in this iOS Development tutorial:

Adding a video, Moving the video to the band detail page

Exercise Preview

ex prev embed video

Exercise Overview

In this exercise, you’ll learn how to embed a YouTube video into the app.

Full-Stack Web Development Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Getting Started

  1. If you completed the previous exercise you can skip the following sidebar. We recommend you finish the previous exercises (1B–3A) before starting this one.

    If you completed the previous exercise, Jive Factory.xcodeproj should still be open. If you closed it, re-open it (from yourname-iOS Dev Level 2 Class > Jive Factory).

    If You Did Not Complete the Previous Exercises (1B–3A)

    1. Close any files you may have open and switch to the Desktop.
    2. Navigate to Class Files > yourname-iOS Dev Level 2 Class.
    3. Duplicate the Jive Factory Ready for Embedding Video folder.
    4. Rename the folder to Jive Factory.
    5. Open Jive Factory > Jive Factory.xcodeproj.

Adding a Video

When a user taps the button in the Bands Detail View Controller, we want to load a video of the band instead of taking users to the Jive Factory website.

  1. In the Project navigator, click on WebViewController.swift.
  2. The new code we are adding is going to replace the current code we have linking to the Jive Factory website. Let’s comment out those three lines of code. Select the code shown below:

    let jiveURL = URL(string: "http://www.thejivefactory.com")
    let myRequest = URLRequest(url: jiveURL! as URL)
    siteWebView.load(myRequest as URLRequest)
    
  3. Press Cmd–/ to comment out the code.
  4. First we need to create a new string to hold the YouTube embedded video HTML code. After the code you just commented out, add the following bold code:

    //   let jiveURL = URL(string: "http://www.thejivefactory.com")
    //   let myRequest = URLRequest(url: jiveURL! as URL)
    //   siteWebView.load(myRequest as URLRequest)
    
          let htmlString = ""
    
          // Do any additional setup after loading the view.
    }
    

    We’ll add the HTML code for the embedded video in between the quotes. Let’s get that code.

  5. Open a web browser and go to: tinyurl.com/nicole-promised
  6. Below the video and view count, click the Share link.
  7. Below that, click Embed. On the right of the pop-up, you should see some HTML code.
  8. Uncheck Show suggested videos when the video finishes.
  9. Click into the HTML code to select it all.
  10. Press Cmd–C to copy it.
  11. Close the web browser window.
  12. Switch back to Xcode.

    We’ve prepared some code for you in a file to make the video flexible (responsive). Unfortunately YouTube doesn’t include this.

  13. Go to File > Open (or hit Cmd–O).
  14. Navigate to Desktop > Class Files > yourname-iOS Dev Level 2 Class > Code Snippets and open flexible-video.txt.
  15. Paste the YouTube code in a new line after the existing code. Notice there are some similarities in the two blocks of code, but we’ve replaced the height and width with some code that makes the video flexible.
  16. We only need the link to the video from the code YouTube provided. In the YouTube code (that you pasted at the bottom) select just the link to the video:

    //www.youtube.com/embed/Go9k14yrxeQ?rel=0
    
  17. Press Cmd–C to copy it.
  18. In the code above that was provided, highlight PASTE_LINK_HERE to select it (make sure http: is NOT selected—we need that to make the video show up):

    <html><body><iframe style=\"position:absolute; top:0; left:0; width:100%; height:100%;\" src=\"http:PASTE_LINK_HERE\" frameborder=\"0\" allowfullscreen></iframe></body></html>
    

    NOTE: Previously, YouTube automatically provided http: in its embed code. Now they are giving developers the choice to use http: or the more secure https:. If you accidentally typed over http:, replace it so the video will be viewable.

  19. Press Cmd–V to paste the link over it.
  20. The code should now look like it does below. Note the slash at the end of the YouTube URL:

    <html><body><iframe style=\"position:absolute; top:0; left:0; width:100%; height:100%;\" src=\"http://www.youtube.com/embed/Go9k14yrxeQ?rel=0\" frameborder=\"0\" allowfullscreen></iframe></body></html>
    
  21. Select the code shown above. Don’t select the YouTube-provided code at the bottom, just get the top code!
  22. Press Cmd–C to copy it.
  23. Close the file.
  24. Place the cursor in between the quotes in the let htmlString = "" line of code.
  25. Paste the code so you end up with this:

    let htmlString = "<html><body><iframe style=\"position:absolute; top:0; left:0; width:100%; height:100%;\" src=\"http://www.youtube.com/embed/Go9k14yrxeQ?rel=0\" frameborder=\"0\" allowfullscreen></iframe></body></html>"
    
  26. Next we need to load up the HTML string directly into the siteWebView. Add the following bold line of code (feel free to replace the comment line about additional setup):

    let htmlString = "<html><body><iframe style=\"position:absolute; top:0; left:0; width:100%; height:100%;\" src=\"http://www.youtube.com/embed/Go9k14yrxeQ?rel=0\" frameborder=\"0\" allowfullscreen></iframe></body></html>"
    
    siteWebView.loadHTMLString(htmlString, baseURL: nil)
    }
    

    NOTE: While baseURL is required, we’re not using it so we set it equal to a nil value. If all your links start with the same base URL (such as http://www.youtube.com) you could include it. Then links in the web view don’t need to repeat that same base address as well. While we could have used it in our example, we think it’s better to see complete youtube.com URLs in the code, rather than splitting them apart.

  27. Hit Cmd–S.
  28. In the Project navigator, click on Main.storyboard.
  29. Scroll over to the Bands Detail View Controller.
  30. Double–click on the name of the Jive Factory Website button, change the name to Play Video and hit Return. Don’t worry about the positioning for now.
  31. Click the Run button.
  32. When the Simulator finishes loading, click one of the band listings.
  33. Click the Play Video button.
  34. You should see the Web View Controller now has a video preview. Click on it to play.
  35. After the video starts playing, click the X button to go back to the Web View Controller.

Moving the Video to the Band Detail

Now that we think about it, it seems unnecessary to go to a whole other view to play the video. Let’s add the video directly in the Bands Detail View Controller.

  1. Switch back to Xcode.
  2. Select the Play Video button on the Bands Detail View Controller and hit Delete.
  3. In the search bar of the Object library object library icon, delete any existing text and type: web
  4. Drag WebKit View below the Description. Don’t worry about placement, we’ll set that next.
  5. With the WebKit View still selected in the Editor, in the Utilities area on the right, click on the Size inspector tab size inspector icon.
  6. In the Size inspector set the following:

    X: 20
    Y: 267
    Width: 278
    Height: 156
  7. Click on the Attributes inspector tab attributes inspector icon.
  8. Click the Adjust Editor Options icon assistant editor icon in the top right of the window and choose the Assistant option. We want to see the storyboard showing on the left next to BandsDetailViewController.swift on the right.
  9. If BandsDetailViewController.swift isn’t showing on the right side, click the menu bar at the top of the file and choose it.
  10. We are going to add the WebKit View directly to this View Controller, so below the import UIKit statement add the following bold code:

    import UIKit
    import WebKit
    
  11. Let’s create an outlet for the WebKit View on this page. Hold Control and drag from the WebKit View element in the Editor to the BandsDetailViewController.swift file below the last IBOutlet.

  12. Set the following:

    Connection: Outlet
    Name: videoWebView
    Type: WKWebView
    Storage: Weak
  13. Click Connect.
  14. Now we need to move the video code. We can copy and paste the code we already have from the WebViewController.swift file. In the Project navigator, click on WebViewController.swift.
  15. Select the embedded video code in the viewDidLoad method shown:

    let htmlString = "<html><body><iframe style=\"position:absolute; top:0; left:0; width:100%; height:100%;\" src=\"http://www.youtube.com/embed/Go9k14yrxeQ?rel=0\" frameborder=\"0\" allowfullscreen></iframe></body></html>"
    
    siteWebView.loadHTMLString(htmlString, baseURL: nil)
    
  16. Hit Cmd–C to copy it.
  17. Go to BandsDetailViewController.swift.
  18. Find the viewDidLoad method (around line 24).
  19. At the end of the method, paste the code you just copied:

       bandImage.image = UIImage(named: currentBandDetail!.fullImageName!)
    
       let htmlString = "<html><body><iframe style=\"position:absolute; top:0; left:0; width:100%; height:100%;\" src=\"http://www.youtube.com/embed/Go9k14yrxeQ?rel=0\" frameborder=\"0\" allowfullscreen></iframe></body></html>"
    
       siteWebView.loadHTMLString(htmlString, baseURL: nil)
    }
    
  20. Change siteWebView to videoWebView:

    videoWebView.loadHTMLString(htmlString, baseURL: nil)
    
  21. Click the Run button.
  22. When the Simulator finishes loading, click on a band listing.
  23. Now the video should load directly in the detail view. Click it to launch the video. Awesome! In the next exercise, we’ll load different videos for each of our bands.
  24. After the video starts playing, click X to stop it.
  25. Switch back to Xcode.

Cleaning Up

  1. Now that we’re no longer using the Web View Controller, let’s delete it. In the Project navigator, click on Main. (If you don’t see it, click the Project navigator tab project navigator icon first.)
  2. Click the top of the Web View Controller to select it. (Be sure to get the controller that’s below the Bands Detail View Controller, not the Web View!) The controller should become outlined in blue.
  3. Hit Delete to remove it.
  4. Notice in the Project navigator, we still have WebViewController.swift. Select it.
  5. Hit Delete.
  6. When asked about moving the files, click Move to Trash.
  7. Hit Cmd–S to save.
  8. Keep this file open. In the next exercise, we’ll add a different YouTube video for each band.

Noble Desktop Publishing Team

The Noble Desktop Publishing Team includes writers, editors, instructors, and industry experts who collaborate to publish up-to-date content on today's top skills and software. From career guides to software tutorials to introductory video courses, Noble aims to produce relevant learning resources for people interested in coding, design, data, marketing, and other in-demand professions.

More articles by Noble Desktop Publishing Team

How to Learn iOS & Web Development

Master iOS development, web development, coding, and more with hands-on training. iOS development involves designing apps for Apple mobile devices with tools like Xcode and SwiftUI.

Yelp Facebook LinkedIn YouTube Twitter Instagram