Thursday, 8 March 2012

Tutorial #2: Playing a Video in a iOS App

Step 1: Import the MediaPlayer.framework

In your project, select the main project file and then the "Build Phases" tab. Expand the "Link Binary with Libraries" section and hit the "+". Select the MediaPlayer.framework and click "Add".

Step 2: Create an instance variable in your view controller to hold your media player.

Make an import statement at the top of your view controller for the media player framework.

#import "MediaPlayer/MediaPlayer.h"

Next, using the storyboard and split-screen view (see previous tutorial) add an IBAction for a button in your view that you would like to play a video. Give it a descriptive name.

Lastly, create a variable to hold your media player.

@property (strong, nonatomic) MPMoviePlayerViewController *player;

Step 3: Implement your video player method

Your video player method should look like this.

- (IBAction)playVideo:(id)sender {
    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"yourVideo" 
    player = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL
    [self presentMoviePlayerViewControllerAnimated:player]; 

The above code will grab a path for your video file (in this case, yourVideo.m4v). Then, it will instantiate a new MPMoviePlayerViewController object called "player" with the video file. It then presents that view controller to the user.

That's it. Simple :)

A couple tips. If you're using this code and find that it's not working for you, it is possible that your path isn't being set right. Sometimes, you need to manually identify what resources you would like build into your app bundle. If you're getting a mystery SIGABRT when you try to play your video, this could be why. To make sure your video is being bundled, select your main project file and click on "Build Phases". Expand the "Copy Bundle Resources" item and check to see if your video is listed. If it isn't, just use the "+" at the bottom to add it in.

Happy coding!

Video Player Working!

The video player was laughable easy to implement. Literally, it was six lines of code in the implementation file of the main view controller and one in the header.

Tutorial #1: Implementing a Simple PDF Viewing in a iPhone or iPad Application

Viewing a file in an iOS application has become common place. It's likely, if you're developing for iOS that you're going to run into the need to create simple viewer. The method I'm going to show you will work for any file that can be displayed in a web browser. It's a simple method for displaying simple files. If you want to display something more complex (e.g. Word documents) you're going to need a different technique.

I used this technique to create the PDF viewer in my app.

Step 1: Create a new UIView and UIWebView in the Storyboard.

Simply drag a new UIViewController item to the storyboard. Then, drag and drop a UIWebView item onto your view. This will be where your file is displayed.

Step 2: Create the navigation items.

On your parent view, create any navigation items you would like (e.g. a UIButton) that will take your user to the viewer.

Step 3: Connect your navigation item to your UIView.

Control click and drag from your button to your UIView to create a segue. Select whichever segue works best for your project (e.g. push).

Step 4: Create and connect your file viewer class.

Press "Command + N" or selected File > New File and create a new "Objective-C Class". Make it a subclass of UIViewController and name it something descriptive (e.g. viewerViewController).

Next, go into the storyboard and select your newly created UIView by selecting the view controller icon at the bottom. Then, open the Identity Inspector (in the right sidebar) and select your custom class from the drop down menu at the top.

Step 5: Setup your viewerViewController.h

Depending on the architecture of your app, this may vary. If you are grabbing a local file from in the view and you don't need to have any other view pass the item in, you can simply do everything in your viewerViewController class.

Go back to the storyboard and open up the split-pane view (the little tuxedo icon in the top-right). Now, control click and drag from your UIWebView into your viewerViewController.h file between the @interface and @end tags. This will create an IBOutlet for your web view and set up the @synthesize automatically. Give it a name that's descriptive (e.g. fileWebView).

Step 6: Setup your viewerViewController.m

Now the meat of the project.

In the viewDidLoad method, add the following code.

   NSString *urlAddress = [[NSString alloc] init];
   urlAddress = [[NSBundle mainBundle] pathForResource:@"yourFilename"
   NSURL *url = [NSURL fileURLWithPath:urlAddress];
   NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
   [self.pdfWebView loadRequest:requestObj];

This code will first create a string and then add the path to your file (in this case, the file would be "yourFilename.yourFiletype"). It then creates a file URL with the path. The URL is used to make a request that can be sent to the UIWebView object. Finally, the request is sent to the UIWebView object and your file is displayed!

If you had some information to pass along to the viewer from the previous scene, you could do that by implementing the "prepareForSegue" method in the view controller implementation file for the previous scene.

Hope this helped!

PDF Viewer Update #3

So, with some help from Simon Allardice, I have implemented the PDF viewer using the UIWebView and the Storyboard without any hacks or workarounds :)

Tutorial up next!

Wednesday, 7 March 2012

PDF Viewer Update

So, I finally got the PDF viewer to work! It took some help from the friendly folks at Stack Overflow (thanks Malek_Jundi!) but it's working perfectly.

For help with making a UIWebView see the Stack Overflow question.

Step 2, making the video player!

Tuesday, 6 March 2012

Introductions are in order...

For anyone stumbling upon this blog. I have decided to document my foray into iOS development. I'm not going to promise anything spectacular but I'll try and post about my frustrations, successes, etc. in the coming months.

A little info about me. I have very little coding experience. I took a course during university in C programming but that was about 3 years ago so I have my work cut out for me. As of now, I have worked through the Objective-C Essentials and iPhone SDK Essentials courses offered at and I feel like I have a tentative grasp on the core concepts of programming for the iOS platform.

Currently, I'm working on a learning platform for the iPad. I have completed the initial window that displays a selection of "lessons" and shows you information about them when you select it from the library. My current task is to figure out how to create a view that will show a PDF. Once I do that, I'll try and post a little how-to for anyone who wants to do it in the future. There really isn't much out there on this topic; especially for iOS 5. I've found some pre-existing PDF readers that are open source but they're too complex for my purpose. All I really need to do is show a PDF and be able to scroll through it.

I've narrowed it down to a couple possibilities. The first I'm going to try is using a UIWebView to show the PDF. That should allow scroll and zoom without any extra coding and since my PDFs are going to be fairly simple and small, it should be ok. If not, I'll have to use a different method.

I'll post again when I get it working :)

PS. If you're reading this and your name is Simon Allardice. I owe you a huge thanks for making really awesome courses!