10

when my app starts music is playing:

-(void)playBgMusic {

NSString *path = [[NSBundle mainBundle] pathForResource:@"bgmusic" ofType:@"aif"];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];    }

but he should be able to turn the music off by pressing a button if he presses the button again the music should turn on again. i have:

-(IBAction)check {


if (isquiet == NO) {

    [theAudio stop];

    isquiet = YES;

     defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:YES forKey:@"stringKey"];


}

else {

    [self playBgMusic];

    isquiet = NO;

    defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:NO forKey:@"stringKey"]; } }

I think I didn't get it. Now it works in my first ViewController that I can turn the music on and off but when I go to another viewController while the music is playing, then back again and press the button, the music doesn't stop and when i press it many times the music is played a second time and overlaps.

What's still wrong?

Leon
  • 417
  • 3
  • 11
  • 24

4 Answers4

38

No need to wrap it in an NSNumber, there are some convenience methods for this:

To set a BOOL, use:

[userDefaults setBool:YESorNO forKey:@"yourKey"];

To access it, use:

[userDefaults boolForKey:@"yourKey"];

[EDIT TO ANSWER YOUR ADDITIONAL QUESTION]

Not sure why you are using NSUserDefaults - it seems unnecessary for what you are trying to achieve? Here's what I would do for a button that can start/stop music:

-(IBAction)check 
{
    if (isQuiet)
    {
        // Play music
        // Change the button to indicate it is playing...
    } else 
    {
        // Stop music
        // Change the button to indicate it has stopped...
    }
    // Set your isQuiet to be the opposite of what it was when the button was clicked
    isQuiet = !isQuiet;
}
Rog
  • 18,602
  • 6
  • 76
  • 97
  • saving works but now there's still a problem, i edited my question – Leon Apr 07 '11 at 21:19
  • That's a different question but I've edited my answer, see if it helps. It doesn't look like you need to use NSUserDefaults at all. – Rog Apr 07 '11 at 21:31
  • i explained the necessity in my question before i edited it, sorry for deleting it. I have different viewcontrollers. For expample the app starts, the music is playing and I go to another viewcontroller and back again. in this case the bool is still no. so when I press the button at this time the music would start a second time and overlaps. you understand? – Leon Apr 07 '11 at 21:39
  • thank you, I marked your answer as right and asked another question here: http://stackoverflow.com/questions/5588209/bool-saving-problem than maybe you could take a look at it, would be nice, thanks. – Leon Apr 07 '11 at 22:04
  • 1
    For accept changes you need use `[userDefaults synchronize];` after setting value – Viktor Jul 13 '16 at 14:18
6

Box your BOOL value to NSNumber object and add it to NSUserDefault:

NSUserDefaults *boolUserDefaults = [NSUserDefaults standardUserDefaults];
[boolUserDefaults setObject:[NSNumber numberWithBool:isquiet] 
                     forKey:@"stringKey"];

Later you'll be able to retrieve that value as plain BOOL using -boolForKey: function in NSUserDefaults

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • there's also a `setBool` function so wrapping it in a NSNumber is unnecessary :) – Rog Apr 07 '11 at 21:00
1

Swift:

To save bool:

UserDefaults.standard.set(true, forKey: "storageKey")

To retrieve the bool:

let boolValue = UserDefaults.standard.bool(forKey: "storageKey")
Bankim
  • 254
  • 4
  • 14
1

To save:

[boolUserDefaults setObject:[NSNUmber numberWithBool:isQuiet] forKey:@"stringKey"];

When you read it back, read it as a NSNumber then do:

BOOL savedIsQuiet = [theNumberYouSaved boolValue];
Terry Wilcox
  • 9,010
  • 1
  • 34
  • 36