Embedding Video into the App
Master video embedding in iOS applications step by step
What You'll Learn in This Tutorial
YouTube Video Integration
Learn to embed YouTube videos directly into your iOS app using WebKit views and HTML strings.
UI Component Management
Master moving and positioning WebKit views within your app's interface for optimal user experience.
Code Organization
Practice proper cleanup techniques by removing unused view controllers and maintaining clean project structure.
This tutorial builds on exercises 1B through 3A. If you haven't completed the previous exercises, use the provided Jive Factory Ready for Embedding Video folder to get started.
Project Setup Process
Navigate to Class Files
Close any open files and navigate to Desktop > Class Files > yourname-iOS Dev Level 2 Class
Duplicate Project Folder
Duplicate the 'Jive Factory Ready for Embedding Video' folder and rename it to 'Jive Factory'
Open Xcode Project
Navigate to Jive Factory folder and open Jive Factory.xcodeproj to begin the exercise
YouTube's default embed code doesn't include responsive design. This tutorial provides custom HTML/CSS code to make videos flexible and properly sized for mobile screens.
Video Embedding Implementation
Comment Out Existing Code
Select the three lines linking to Jive Factory website and press Cmd-/ to comment them out
Create HTML String
Add 'let htmlString = ""' after the commented code to hold the YouTube embedded video HTML
Get YouTube Embed Code
Navigate to the YouTube video, click Share > Embed, uncheck suggested videos, and copy the HTML code
Implement Responsive Design
Use the provided flexible-video.txt code and paste only the video link portion into the responsive template
Video Placement Options
| Feature | Separate Web View Controller | Embedded in Detail View |
|---|---|---|
| User Experience | Requires navigation | Immediate access |
| Code Complexity | Multiple controllers | Single controller |
| Performance | Additional view loading | Direct integration |
| Maintenance | More files to manage | Streamlined structure |
WebKit View Integration
Add WebKit Import
Import WebKit framework in BandsDetailViewController.swift to enable WebKit view functionality
Configure WebKit View
Set precise positioning with X:20, Y:267, Width:278, Height:156 for optimal mobile display
Create Outlet Connection
Control-drag from WebKit view to create IBOutlet named videoWebView of type WKWebView
Transfer Video Code
Copy HTML string and loadHTMLString code from WebViewController to BandsDetailViewController
Project Cleanup Tasks
Removes unnecessary UI components that could confuse users
Eliminates dead code and reduces project complexity
Permanently removes the file from your project directory
Press Cmd-S to ensure all modifications are preserved
You've successfully embedded a YouTube video directly into your iOS app's detail view. The next exercise will show you how to load different videos for each band in your app.
Key Takeaways
