1

Developing for a jailbroken device.

I've looked here for a soultion to my problem. Using NSUserDefaults for storing UISwitch state but people say alot of the codes don't work.

I'm using a UISwitch to load/unload a launch daemon for iOS. I've gotten very close but the switch state won't save. This is the code im using.

@synthesize toggleSwitch;

- (void)viewDidAppear:(BOOL)animated
{

[super viewDidAppear:animated];

if([[NSUserDefaults standardUserDefaults] boolForKey:@"switch"]) toggleSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"switch"];

}

- (void)switchValueChanged {
if (toggleSwitch.on) {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
    switchlabel.text = @"Enabled";
    const char *onchar = [[NSString stringWithString:@"launchctl load -wF /System/Library/LaunchDaemons/com.launch.daemon.plist"] UTF8String];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
    setuid(0); system(onchar);
    if (system(onchar) == 0){
        [[NSUserDefaults standardUserDefaults] setBool:self.toggleSwitch.on forKey:@"switch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    } else {
        [[NSUserDefaults standardUserDefaults] setBool:self.toggleSwitch.on forKey:@"switch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];}
} else {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
    switchlabel.text = @"Disabled";
    const char *offchar = [[NSString stringWithString:@"launchctl unload -wF /System/Library/LaunchDaemons/com.launch.daemon.plist"] UTF8String];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
    setuid(0); system(offchar);
    if (system(offchar) == 0){
        [[NSUserDefaults standardUserDefaults] setBool:self.toggleSwitch.on forKey:@"switch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    } else {
        [[NSUserDefaults standardUserDefaults] setBool:self.toggleSwitch.on forKey:@"switch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE]; }
}

The bool value is written in my app plist. I don't seem to know what I did wrong. I pieced afew examples from questions on overflow with no luck. Regardless if the command fails, the switch should save.

Could someone explain and show me what should be edited. This has been driving me absolutely nuts.

Community
  • 1
  • 1
CokePokes
  • 941
  • 3
  • 12
  • 25

0 Answers0