Posted 11 months ago

Moved to Posterous

This blog has moved platforms to Posterous. You can continue to access it at http://dev.orangegroupapps.com

Posted 1 year ago

Play Audio on the iPhone

I want to teach you how to play audio files on the iPhone by clicking a button. I will show you the correct way to handle memory management when playing an audio file (since this often is not addressed).

I will skip the uber-basics of creating an iPhone App since it has been done over and over. If you need to learn from square 1, I suggest reading iPhone and iPad Apps for Absolute Beginners.

First, create a project. I called mine Audio. Next, add the AVFoundation framework. Then, #import <AVFoundation/AVAudioPlayer.h> into your header file. Make your header file implement the <AVAudioPlayerDelegate>.

Create a pointer to an AVAudioPlayer object: AVAudioPlayer *audioPlayer;

Declare the property for the audioPlayer: @property (nonatomic, retain) AVAudioPlayer *audioPlayer;

Declare a method in the header file: - (void)playAudio;

Synthesize your audioPlayer in the .m file: @synthesize audioPlayer;

Be certain to release the audioPlayer in the dealloc method. It is a good idea to use the dealloc method to release any object that has been synthesized.

Finally, we implement the method for 

- (void)playAudio {

    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@”ha” ofType:@”m4a”];

    AVAudioPlayer *newAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath isDirectory:NO] error:nil];

    self.audioPlayer = newAudio;

    [newAudio release];

    [audioPlayer setDelegate:self];

    [audioPlayer play];

}

We create a NSString which specifies the file name “ha”, it’s type “m4a”, and location: in the main bundle. This is a sample sound from our Hawaiian Names & Hawaiian Words Apps.

The key to this tutorial is memory management, which is why we now create a new AVAudioPlayer pointer object called newAudio and initialize it with the contents of our soundPath (but in URL format as AVAudioPlayer requires).

We can then set audioPlayer equal to newAudio and release newAudio from memory (since it was allocated, we are responsible for releasing it).

Next, set the delegate of audioPlayer to this class (we added the AVAudioPlayerDelegate to the header).

Finally, play the sound.

In order for this App to work, you must connect a button or some other action to call the - (void)playAudio method. This is covered in the book I mentioned.

Why do we create audioPlayer, and not just newAudio? Because we would need to release newAudio somewhere within the method, and if we release it right after we play, the sound gets clipped. There are cases when we could release the AVAudioPlayer object after its BOOL property isPlaying is no longer true, but the structure I have presented allows more flexibility in playing multiple sounds at once and sequentially, such as in our app: Animatopoeia.

Full code below:

Posted 1 year ago

How to create a “Rate My App” prompt for iPhone & iPad

While many users may use and enjoy your app, few users will make the effort to rate it. Even worse, sometimes only those motivated by a passionate dislike of your app will take that effort. To remedy this, I decided to create a prompt to ask users to rate my app.

Take a few things into account:

1. You don’t want it to pop up the first time they launch it, because they haven’t used your app yet. I decided to have it pop up the third time the app is opened. I used an integer called “launchCount” to measure this. (Note: on multi-tasking phones, it must be closed then re-opened to increase the count). 

2. You don’t want to annoy the user. Have the ability to never show the prompt again. (Boolean “neverRate”).

3. If they do click the “Rate my App” button, you should also never show it again after that.

4. They may want to rate your app, but not right now. Give them the option to remind them the next time the app is opened.

I placed the code in the App Delegate. Inside the applicationDidFinishLaunching method, I simply call [self rateApp]; Be sure to add the UIAlertViewDelegate to the header file. You may copy the following code wholesale, but be sure to change the URL to your own iTunes URL (otherwise you’ll be linking to my app: Hawaiian Names), and change the title and message of the alert view.

The - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex method  handles “buttonIndex” as a integer in the order of the buttons you added to your alert. Starting, of course, at 0, which corresponds to “Rate my App”, we make sure the prompt will never pop up again, then send the user to our app page to rate it. At “buttonIndex” 1, which corresponds to Never ask again, we set the “neverRate” BOOL to YES. At 2, which corresponds to “Remind me later”, we do nothing, and the prompt will pop up again the next time the app is launched. 

Posted 1 year ago

1000 Lockers Problem

My sister’s friend proposed a logical/mathematical puzzle today that I decided to solve algorithmically. There may be a more efficient way to solve it, and after figuring it out with the program, I found a better way to think about the problem, but I wanted to post the code that I wrote this afternoon to find the answer.

Here is the puzzle: Imagine 1000 lockers in a school. They all start out closed. There are also 1000 people, each given a number corresponding to one of the 1000 lockers. They then go to their locker number (e.g. Locker #14) and each one of it’s factors (e.g. 7, 2, 1) and open it, or close it (if someone else had already opened it). The question is: How many lockers will be open once every person (1 through 1000) has opened or closed their locker number and every factor of that number.

I’ll post the answer at the end in case you want to solve it for yourself. Here is my code:

The answer is 31. It is the number of perfect squares from 1 to 1000.

Posted 1 year ago

Business cards & a privacy candy cane (Taken with Instagram at El Camino Park)

Posted 1 year ago

Set Region Center to Current Location

I’m working on my first contract project which requires a map to show the location of certain venues & events. It will have the ability to locate nearby events by using the user’s location and provide them with search directions to that location.

For some reason, one of the most frustrating parts was what should be quite simple. Starting out the MapView centered on the user’s current location. For everyone’s benefit, here is the code that worked:

Posted 1 year ago

UISearchBar & Custom Table Cells

I’ve been working on an iPhone app that involves using the search bar. This is my first time using the search bar, so I’ve collected bits of code here and there from tutorials and reading questions on Stack Overflow. I can’t find the original tutorial that I used, but this one is strikingly similar.

The problem I was having is that I am using a Custom Cell for the Table View which displays data from 2 different dictionaries. A primary label, subtitle, and an image:

The - (void)handleSearchForTerm:(NSString *)searchTerm method from the tutorial was built to search and present data from a single NSMutableDictionary. It was successfully eliminating the data from the dictionary containing the primary label when the search terms changed, but not eliminating the data from the secondary label’s dictionary and the image file dictionary (e.g. when I typed “V”, werewolf and ghost were eliminated from their respective dictionary, but not “Howls at the moon” and “Is transparent and haunts you”. And also the werewolf and ghost images were not removed). The result was that when I searched “Vampirism”, the primary label “Vampire” displayed, but the secondary label and the image displayed were from the first entries in their respective dictionaries. I could search “Vampirism” and it would display a picture of a centaur (the first entry in the dictionary), the primary label “Vampirism” and the secondary label “Half-man, half-horse”.

After probably over 20 hours of struggling with this, I finally found a solution. It’s not elegant, and I still am not able to search by both “Monster Name” and “Monster Description”

The solution was to create an NSMutableIndexSet to get the index numbers that correspond to the monster being removed from the “monsters” NSMutableDArray. I could then use that index set to remove the objects from the other two arrays.

Read More

Posted 1 year ago

Introduction

My name is Michael Patzer and I am an iPhone developer for Orange Group Apps. I started learning Objective-C in October, 2010 with no programming background. I started with Neal Goldstein’s books Objective-C for Dummies and iPhone Application Development All-in-One for Dummies. Even the Dummies books were above me, and I didn’t really progress until reading Rory Lewis’ iPhone and iPad Apps for Absolute Beginners. For anyone just starting out, I highly recommend that book.

I am creating this website to help out beginner iPhone programmers.