Phillip
Phillip

Reputation: 4306

Reload contents at startup

I've been banging my head against the wall for several days trying to understand how to perform an action as soon as the application starts.

Basically I want to download a plist from my website if the user turns on a switch that determines if he wants to download new contents at startup.

Point is that:

Now, I don't know how to tell the AppDelegate to run the method of class "A" if the switch of class "B" is turned on. Obviously I need to use NSUserDefaults, but i'm pretty lost after that.

Can anyone make things clearer? Or, is there a more comfortable workaround to do it?

Upvotes: 1

Views: 108

Answers (2)

janusfidel
janusfidel

Reputation: 8106

yes you can do this using NSUserDefaults

in your class b.

-(void)swithChanged
 {
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //check if !null
   if(![[defaults objectForKey:@"shouldDownload"]isKindOfClass:[NSNull class]]){
         if([(NSNumber*)[defaults objectForKey:@"shouldDownload"]boolValue])
          {
             [defaults setObject:[NSNumber numberWithInt:0] forKey:@"shouldDownload"];
             [defaults synchronize];
          }else{
             [defaults setObject:[NSNumber numberWithInt:1] forKey:@"shouldDownload"];
             [defaults synchronize];

         }
     }else{
       //set your NSUserDefault here for the first time
    }

}

in your AppDelegate

- (void)applicationDidBecomeActive:(UIApplication *)application{
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //check if !null
   if(![[defaults objectForKey:@"shouldDownload"]isKindOfClass:[NSNull class]]){
         if([(NSNumber*)[defaults objectForKey:@"shouldDownload"]boolValue])
          { 
              //you can write the downloadData method in this appDelegate,
             //[self downloadData]

             //OR
             AClass *aClass = [AClass alloc]init];
             [aClass downloadData];
          }else{
            //do not download
         }
     }else{
       //the default behaviour of app, download or not?
    }

}

Upvotes: 2

Related Questions