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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With