Seeding a new app with sample data

So I’ve got an iOS app that, when launched, checks for the presence of a JSON data file in the app’s sandbox Documents folder. If so, it loads the data into memory and then the user can edit or add to this data, which gets saved back to the JSON file as things get changed.

I’ve got a nice sample data file that I built in the simulator and extracted.

What I thought would be cool is, when the app is launched, and the JSON data file is NOT present, that it would load my sample data file instead. Essentially, the first time a user launches the app, they get the sample data, which they can delete and then create their own data.

So I can use a Copy Build Step to copy the sample data file to Resources (I already do this with a font file), but there’s no way I can see to get the app to read something from Resources and copy it to Documents.

Am I correct that I’m hosed?

Not at all.
Copying from resources to documents is what I do all the time.

Name your json file like ‘samplejson.txt’
Drag it into the project at DESIGN time
Now it appears as a resource, and you can access it at runtime as if it was a string constant.

To get it into documents, just create a textoutputstream, open it with the right filename in documents, and .writeline the samplejson variable at it.

You could also use specialfolder.resources.child(“samplejson.txt”) and do a copyfile step

1 Like

Thanks! In the SpecialFolder docs I was looking at the Mobile properties which didn’t list Resources as an option, but now I see that there’s a Resource method I can use. Odd.