tags:

views:

81

answers:

2

I have an iPad application that pulls in all of its data from an external web service. I am working on building in a demo mode that will use a cache of demo data stored on the device so it can be demoed and tried out without the web service connection (or an internet connection.)

Is there a project or good practices standard to follow to model this type of sample data? I'm expecting JSON arrays/Dictionaries back from my web service, how could I build a function that uses hardcoded data to create the NSMutableData object I'd expect to get back from a JSON web request?

+3  A: 

I always stick sample data in a plist file in my resource directory. Obviously it can be created as, say, an array of dictionaries etc, and so can be loaded directly from file:

NSString *myFile = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SampleData.plist"];
NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfFile:myFile];

Equivalent method exists for arrays. Obviously it might be slightly different format to your JSON array but ease of importing wins for me!

h4xxr
+1  A: 

I did this for an app which is basically a front end to a WordPress web site. The app was designed to cache data to the Documents dir automatically, so prior to release, I just pulled out all the cached data I needed from the Simulator's directory for my app, put it in the bundle, and used the following logic:

If (network available ){
    get new live data
}else if (have cached data from previous connection){
    use it.
}else{
    use data from the bundle
}

You might get some compiler errors if you try copy/pasting that into xCode...

wkw