Some times it your application may inadvertently quit with out the user meaning to quit your app. For example people run with the running/GPS app and the iPhone may be in a pocket / arm band / hand where the square button could be pressed unintentionally. If the app quits while running it will not collect running data for the rest of the run and the user will be very disappointed. To avoid this I have setup an alert which will make a sound if the user quits the app while the app is collecting running data. I was able to find the original apple alert aiff files via Google and I chose to implement the Indigo sound as the alert – the cricket is not really loud enough to be an alert sound.
By implementing the playback of the sound as an alert sound the sound will be played even on the 1st gen iPod Touch which does not have a speaker. Alert sounds can also be set to finish playback after the app finishes – which is an important feature for this use. I was able to find how to set this property on Nagano’s blog. Not sure what most of the page says but the language of Objective C is universal. Remember to add the audiotoolbox to your project.
#include <AudioToolbox/AudioToolbox.h>
I then load the sound in the viewDidLoad init function for the view controller where I want to play the sound – this way you are not trying to load the sound as the app is quitting. The soundFileObject isa global for this VC.
// SOUNDS
NSString *path = [[NSBundle mainBundle] pathForResource:@"Indigo" ofType:@"aiff"];
NSURL *fileURL = [NSURL fileURLWithPath:path];
OSStatus err;
err = AudioServicesCreateSystemSoundID((CFURLRef)fileURL, &soundFileObject);
if(err) {
DLog(@"AudioServicesCreateSystemSoundID err = %d",err);
exit(1);
}
UInt32 flag = 1;
err = AudioServicesSetProperty(kAudioServicesPropertyCompletePlaybackIfAppDies,
sizeof(UInt32),
&soundFileObject,
sizeof(UInt32),
&flag);
if(err){
DLog(@"AudioServicesSetProperty err = %d",err);
}
Then under viewWillDisappear I have the following:
if (runActivity.isStarted) {
// play alert sound if exiting while running
AudioServicesPlayAlertSound (self.soundFileObject);
}
This way the sound will play if the view is unloading while the user is expecting the app to be collecting data.


