I have multiple sounds in my application. I want to setup a toggle switch in settings to turn these sounds off. Here is the code that executes the sounds.
- (void) soundEventDidHappen:(NSString*)eventName {
//check dictionary of sounds.. if there is a corresponding sound for this event name, play it
if ([[soundIDForEventString allKeys] containsObject:eventName]) {
AudioServicesPlaySystemSound ([[soundIDForEventString objectForKey:eventName] intValue]);
}
You could make a simple toggle like this:
bool isToggled;
- (IBAction) toggleSound{
if(isToggled){
isToggled = NO; //sets isToggled to false if it's already true
}
else{
isToggled = YES; //sets isToggled to true if it's already false
}
}
and then you could just do:
- (void) soundEventDidHappen:(NSString*)eventName {
//check dictionary of sounds.. if there is a corresponding sound for this event name, play it
if(isToggled){
if ([[soundIDForEventString allKeys] containsObject:eventName]) {
AudioServicesPlaySystemSound ([[soundIDForEventString objectForKey:eventName] intValue]);
}
}
}
Which will play the sound if isToggled
is true. You could then use NSUserDefaults
if you would like to save the boolean for the future:
[[NSUserDefaults standardUserDefaults] setBool:isToggled forKey:@"soundEnabled"];
[[NSUserDefaults standardUserDefaults] synchronize];
//use this to save
Then you could use this to get the value (most likely somewhere in your viewDidLoad
method:
isToggled = [[NSUserDefaults standardUserDefaults] boolForKey:@"soundEnabled"];
[[NSUserDefaults standardUserDefaults] synchronize];