(Previous: Crash Course #2 - Building Your First iPhone Application: Hello World!)
What better way to demonstrate the iPhone’s advanced user interface and state of the art hardware than to roll your own mobile fart application. In this crash course I will expect you understand some of the concepts from Crash Course #1 & #2, but this application is really not much more complex than Hello World from Crash Course #2.
1. Start a new View Based project in Xcode called “iFart”
2. Click on Project -> Add to Project
3. Navigate to /Developer/Platforms/iPhoneSimulator.platform/Developer/ SDKs/iPhoneSimulator2.x.sdk/System/Library/Frameworks then add AudtioToolbox.framework.
4. Do not tick the box to copy to project, and select “Relative to Current SDK” in the drop down menu
5. Open “iFartViewController.h” and paste in the following code
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface iFartViewController : UIViewController {
}
- (IBAction) buttonPressed:(id)sender;
@end
You will notice the code doesn’t have any OUTLETs.. it doesn’t need any as it is not going to update anything on the iPhone screen, it only needs to play a sound so there is only a single ACTION.
6. Open “iFartViewController.m” and add this code after the @implementaion iFartViewController
- (IBAction)buttonPressed:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:@”fart”ofType:@”wav”];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound (soundID);
}
All this code does is play “fart.wav”.
7. Now we need to actually add fart.wav to our project. Find a suitable bit of wav audio less than five seconds and call it fart.wav and drag it to the resources folder of your project. When it prompts you set the drop down to “default” again and tick the box to copy the file to the project.
8. Now double click on “iFartViewController.xib” and go into the Interface Editor. It’s time to add our fart button.
9. Drag a “Round Rect Button” from the Library into your view. Use the handles on the button to resize it to the full size of your window if you like, so the whole screen is one giant farty button. Change the button text to whatever you like.
10. Now we are going to connect the Button to our ACTION. Click on the button then go to the blue arrow toolbar button in the “Button Connections” window and click-drag from the “Touch Inside Up” circle to the FILES OWNER CUBE (pictured).

11. Select our “buttonPressed” method from the list
12. We are done! Save your interface and Build and Go

The Finished Application
NOTE – For some reason the iPhone Simulator may or may not produce sound. I’ve found this code works fine when you run it on the actual iPhone, although it costs you $99 in developer fees for the privilege of signing your code to test on an actual iPhone.
Ok so it’s not exactly the greatest example of Apple’s clean code and Model View Controller methodology, but it’s a simple example that again illustrates an IBAction method call from a button, and in this tutorial we’ve used some new calls from the Audio Toolkit framework to play our wav.
I’ve included the source code bundle for you here.
Have Fun!


















[...] (Next: Crash Course #3 : Make an iPhone Fart Application) [...]
I’d really love to see an App that allows you to play your farts like an instrument. You could hold down an different areas for different tones and have the farts go as long as you are holding it…
That would be a quality farting app.
iPARP
Hehe, I guess it wouldn’t be that hard, just use a piano graphic and hide your buttons behind the keys (invisible buttons) and then record a couple of octaves of farts loops
Hey, great tutorial! But I have a little problem. I can’t drag the Touch Inside Up” circle to the files owner cube. I wont react, and I don’t get any drop down. Do you know what I’m doing wrong?
Thanks
Nicklas,
I had the same problem. What worked for me was to go back into Xcode, save, go back into Interface Builder, then retry the dragging step.
Hope that helps!
I have looked and looked but I get thirteen errors from the ifartviewcontroller.m code.
All from line 15
NSString *path = [[NSBundle mainBundle] pathForResource:@”fart”ofType:@”wav”];
Using build 2.2.1
New to this so be easy on me
13 errors hey! I’d double check that you’ve added the audio framework correctly as it’s failing trying to create the bundle.
Hmmm… I’ve had some problems, wondering if you could help:
All of my own apps that I’ve tried to build compile and open fine, but when I tap the button it turns blue and no sound plays. But your app works fine (the one I downloaded). Is this because I have the free SDK, or am I doing something wrong? It doesn’t work even when I take your app, delete your fart.wav, and add my own fart.wav. Any ideas why?
Hi Alejandro, I suspect it is the .wav format you are giving it, the core audio service is very specific about the kind of audio it will play (bit rate etc). There are other methods to play audio of other formats. I’ve been using “AudioServicesPlaySystemSound” but you have to also declare and initialize your sounds, which is good because they preloaded so are very responsive. Hope this helps!
I have the same 13 errors (and one warning) from the same line as Nicklas. I am also using 2.2.1. I’ll try downgrading or upgrading the audio toolkit version…
Hi Nicklas,
I figured out our problem. Go to the “fart” and the “wav” and delete the quotes before and after each word, then retype them. The ASCII close quote (looks like “99″) is being used by the programmer, which works on his system I guess, but doesn’t on ours. Retype and the two words will turn from black to red. Red means that it is properly recognized…
Sorry, that was CJ that I meant to write the messages to… :S