Question

 defaults = [NSUserDefaults standardUserDefaults];
 NSLog(@"%@",[defaults objectForKey:@"firsttime"])

if([[defaults objectForKey:@"firsttime"]isEqualToString:@"YES"])
{
    UISwitch *onoff = (UISwitch *) sender;
    if(onoff.on)
    {
        NSLog(@"yes on1 facebookswitch");
        facebookSwitch.on = YES;
        [userDefault setValue:@"true" forKey:@"facebooknotify"];

        NSLog(@"on");
        if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
            [validate alertCommonWithoutMessage:NSLocalizedString(@"val_message",nil) :NSLocalizedString(@"val_facebook_login",nil) :@"OK"];
            facebookSwitch.on = YES;
            NSLog(@"yes on2 facebookswitch");

        }
        else {
            NSLog(@"val_facebook_conf");
            [validate alertCommonWithoutMessage:NSLocalizedString(@"val_message",nil) :NSLocalizedString(@"val_facebook_conf",nil) :@"OK"];
            facebookSwitch.on = NO;
            [userDefault setValue:@"false" forKey:@"facebooknotify"];
            NSLog(@"yes off1 facebookswitch");

I'm using this method,but sumtimes the response is null.

Was it helpful?

Solution

Try this code:

// add this code in your switch touch event
- (IBAction)YourSwitch:(UISwitch*)sender
{
    if (UISwitch.on)
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setBool:true forKey:@"Sound"];
        [defaults synchronize];
    }
    else
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setBool:false forKey:@"Sound"];
        [defaults synchronize];
    }
}

// add this code in your viewDidload .
BOOL isSound= [[NSUserDefaults standardUserDefaults] objectForKey:@"Sound"];

if (isSound){
    _ref_slider.on=TRUE;
} else {
    _ref_slider.on=FALSE;
}

i hope this code is useful for you.

OTHER TIPS

As to why it's not working, the most common problem is [userDefault synchronize]

Also, rather than using string boolean values, why not use:

[userDefault setBool:YES forKey:@"facebooknotify"];

and then access by

BOOL notified = [userDefault boolForKey:@"facebooknotify"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top