Aug 302013
 

Lets say you have an app that you don’t quite know what some parameters should be. This isn’t that uncommon, it might be as simple as not knowing what a default should be, or maybe you’ll have a list of in-app content to sell. The possibilities are pretty huge.

This is surprisingly easy to do. First, make a JSON file and host it somewhere.

{ "ShadowHeight":"20" }

Make a property to hold this value and initialize it.

@property(nonatomic, assign) int shadowHeight;
self.shadowHeight = 15;

Next you need to start a background query to get that file from the net.

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://yourdomain.com/yourfile.json"]];
        [self performSelectorOnMainThread:@selector(parseJSONFile:)
                               withObject:data waitUntilDone:NO];
    });

Your app will run with the default value of 15 until the net query returns. When it does, the parseJSONFile: selector is called.

- (void)parseJSONFile:(NSData *)responseData {
    NSError* error;
    if (responseData) {        
        NSDictionary* json = [NSJSONSerialization
                              JSONObjectWithData:responseData
                              options:kNilOptions
                              error:&error];
        
        NSNumber *shadowHeight = [json objectForKey:@"ShadowHeight"];
        self.shadowHeight = [shadowHeight integerValue];
    }
}

And that’s it. We run with the compiled in value and switch to the runtime value when (or if) the file is served by the host. It couldn’t be easier.