Setting the Video for Each Band

Free iOS Development Tutorial

Explore our comprehensive tutorial on iOS Development, covering topics such as defining video URL for band detail objects and making the URL a dynamic string.

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:

Defining the video URL for band detail objects, Making the URL a dynamic string

Exercise Preview

ex prev video done

Exercise Overview

In the previous exercise we embedded a video into the detail view, but it’s the same video for each band! In this exercise, you’ll learn how to pass along the correct video URL, depending on the currently chosen band.

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–3B) 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–3B)

    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 Setting Band Videos folder.
    4. Rename the folder to Jive Factory.
    5. Open Jive Factory > Jive Factory.xcodeproj.

Getting Started

If you remember from a previous exercise, the details for the four bands are contained in objects of the BandDetail class we created. We want to pass along the correct youtube.com video URL for each of these bands. First we need to add a new property to our class to hold the video URL.

  1. In the Project navigator, click on BandDetail.swift.
  2. After the last property declaration add the following code:

    var showDetails:String?
    var videoURL:String?
    
    }
    
  3. Hit Cmd–S to save.

Defining Video URL for BandDetail Objects

Now that the BandDetail class has a new property, we can add the correct video URL to the four BandDetail objects we created in a previous exercise.

  1. In the Project navigator, click on BandsTableViewController.swift.
  2. To save you the hassle of copying and pasting four different URLs, we’ve created a file for you with these four objects defined with the new videoURL code for you to copy and paste. Hit Cmd–O.
  3. Navigate to Class Files > yourname-iOS Dev Level 2 Class > Code Snippets and open objects-with-videoURL.txt.
  4. Press Cmd–A to select all the code.
  5. Press Cmd–C to copy it.
  6. Close the file.
  7. Back in BandsTableViewController.swift, select all the code for the four BandDetail objects:

    let nicoleAtkinsBandDetail = BandDetail()
    nicoleAtkinsBandDetail.bandName = "Nicole Atkins"
    nicoleAtkinsBandDetail.bandType = "Rock"
    nicoleAtkinsBandDetail.bandDescription = "Nicole will knock your socks off."
    

    Code Omitted To Save Space

    blackAngelsDetails.nextShowDate = "Sun 5/6"
    blackAngelsDetails.nextShowTime = "8pm"
    blackAngelsDetails.venue = "Cake Shop"
    blackAngelsDetails.showDetails = "Over 21 - $15"
    
  8. Press Cmd–V to replace the code with the prepared code you copied.
  9. Find the code where we define the videoURL for Nicole Atkins:

    nicoleAtkinsBandDetail.videoURL = "http://www.youtube.com/embed/Go9k14yrxeQ?rel=0" 
    

    Notice it’s the same as the other string definitions. The code you pasted is exactly the same as the previous code, except we added a unique videoURL for each band.

  10. Hit Cmd–S to save.

Making the URL a Dynamic String

  1. Now we need to get it so that the correct video is being shown for each band. In the Project navigator, click on BandsDetailViewController.swift.
  2. Find the code that defines the htmlString for the video.

    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>"
    

    We are going to be replacing the current youtube.com link with the one that gets passed depending which band the user taps in the app. If you remember from a previous exercise, we use the currentBandDetail object to hold that info. There are a few steps we have to do to be able to add the videoURL property to our string. We’ll be using string interpolation to dynamically construct a new videoURL.

  3. Select just the URL of the video (shown in bold):

    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>"
    
  4. As shown below, replace the URL with the bold code to use the video URL variable within the htmlString constant:

    let htmlString = "<html><body><iframe style=\"position:absolute; top:0; left:0; width:100%; height:100%;\" src=\"\(currentBandDetail!.videoURL!)\" frameborder=\"0\" allowfullscreen></iframe></body></html>"
    
  5. Click the Run button.
  6. When the Simulator finishes loading, click on one of the band listings.
  7. Pay attention to which video loads in the detail view.
  8. Click the Bands button at the top left.
  9. Click on another band. Notice a different video loads. Awesome!
  10. Switch back to Xcode.
  11. Save the file.

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