YouTube Videos and the UITableView Controller (and some best practices)

February 13th, 2012 @   -  One Comment

Download the code here

Something that seems to be a rather common thing these days is the desire to display YouTube videos within an iOS app without changing to the native YouTube player. There’s a great example on www.iOSDeveloperTips.com on this which explains the basic premise. Note that you’ll have to generate a Youtube API Key for your project, and you’d do that here.

Essentially, the idea is to subclass UIWebView and create and load the HTML necessary to play the video right in place (although it doesnt work in the simulator). Very simple!

I thought it would be neat to use this UIWebView within a UITableViewCell to display Youtube videos and their thumbnails with inside a UITableViewController. There’s a post on StackOverflow.com that talks about this, and I decided to write a quick app using storyboards, iOS5, and ARC that does this, and I think its pretty neat so I decided to share, along with a couple of best practices that I include in my own apps.

Some of the things I included are…

  • Data Model
  • ReachabilityManager
  • Externalization of String Resources
  • Custom UITableViewCell
  • Icons

DataModel
In keeping with the MVC design principle, I’m always very sure to separate my Model from the rest of my code. You’ll notice that the DataModel has a delegate to allow me to inform the app of changes to the data. Right now it’s just dummy data, but when you have a real datasource (php, xml, rss, whatever) you can just update it here.

ReachabilityManager
The ability to know if your device is connected to the internet is quite common, but the example app currently provided by Apple is not iOS5 and ARC compliant. Tony Million has written a drop in port to ARC which I’ve leveraged. I’d written a class called “ReachabilityManager” which you can query from the app to ascertain network connectivity easily. It hands off the DataModel for easy access.

String Resources
I’m a big fan of creating .strings files to maintain my external strings. There’s nothing quite as annoying as having to recompile my app just to change a string resource.

Custom UITableViewCell
The method for creating custom Table View Cells has really changes a lot since iOS4, and of course I needed to customize in order to display a UIWebView (youtube video), so you can check that out as well.

Icons
I’ve included all the currently needed Icon sizes for inclusion of an app into the app store. I always find myself searching for the Human Interface Guidelines for this stuff

Hopefully you’ll find this stuff informative and useful.

Here’s how it looks

 

 

 

 

 

 

 

 

 

 

Other great and useful links
Stack Overflow – One of the best places to get any development question answered… and promptly!
iOS Developer Tips – another place for great insight and examples. Top notch!
Ray Wenderlich’s Site – Ray is a paragon of giving back to the development community on year he did an April fools joke saying he was giving up iOS and I almost threw up before I realized it wasnt true. True fact. Thank you for all you do, Ray!
Tony Million – Another great iOS Developer you should keep an eye on. Here’s his blog post talking about his Reachability project I’m

More General / Documentation Type Sites
iOS Developer Center
iOS Provisioning Portal
Apple Developer Forums
iTunes Connect
Apple Developer Support
Sample Code (Updated for iOS 5)
Official Guide to App Submission

  • Dragos Gavrilovici

    it’s good post, and you explain is very good. When i try to Run 

    ‘SHK.h’ file not found

    Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang failed with exit code 1