Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use an Xcode build script to download JSON files to the app bundle?

I have a web server, running locally, which serves JSON-formatted data from a number of endpoints. I currently include the data from each endpoint in separate .json files, which I manually add to the app bundle for use within the app. Is it possible to automate this process whenever the project is built, perhaps using an Xcode build script?

Below is an example of what I am trying to achieve.

  1. Fetch the JSON-formatted data from localhost:3000/example.
  2. Stop here if the endpoint cannot be reached.
  3. Save the data in a file called example.json.
  4. Add example.json to the app bundle for use within the app.

Any help with this would be greatly appreciated.

Edit

I have fetched the JSON-formatted data, however I am now looking to see how this data can be copied into the app bundle.

curl -o example.json http://localhost:3000/example
like image 650
Andrew Sula Avatar asked Sep 06 '14 16:09

Andrew Sula


1 Answers

Here's how I did the same thing in my project.

I went to my target -> Build Phases -> + -> 'New Run Script Phase' and added the following script:

curl -o ${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/your_file.json http://localhost:3000/your_file.json
echo "Your file downloaded to ${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/your_file.json"

I am currently working on adding some validation to the process, but as a starting point this is working for me.

Hope that helps!

UPDATE

Here is the same code with some added JSON validation:

YOUR_DATA=$(curl -s "http://localhost:3000/your_file.json" | python -m json.tool);

if [ -n "$YOUR_DATA" ];
then
echo "$YOUR_DATA" > ${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/your_file.json;
fi
like image 154
poff Avatar answered Oct 05 '22 23:10

poff