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

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


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.jsonecho "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" ];thenecho "$YOUR_DATA" > ${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/your_file.json;fi


Not sure if I'm getting your specifications right here but this is what I have implemented as well as some help from this solution: How can I save a JSON response to a file that would be accessible from within a local HTML file loaded inside UIWebWiew

(1) Here is the function that will grab the json file. This function is basically assigningthe url you are targeting to a string. Then formatting it to a NSURL. This is then formatted as NSData which will grab the contents of the url. The data will be serialised andthen ready to target the appropriate elements.

    - (void)fetchJson       {           NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);        NSString *documentsDirectory = [paths objectAtIndex:0];        NSString *urlString = @"localhost:3000/example";        NSURL *url = [NSURL URLWithString:urlString];        NSData *jsonData = [NSData dataWithContentsOfURL:url];        NSError *error = nil;        if(jsonData != nil)        {            NSError *error = nil;            id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];            if (error == nil)                NSLog(@"%@", result);           // (2) This part is dependent on how your json data is arranged (Target the            // right elements).           _endpoint = [result objectForKey:@"endpoint"];           if(_endpoint == null){                  NSLog(@"%@", result);           }           else{               //(3) This will save your data into the specified file name under the application documents directory.              NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"example.json"];             [result writeToFile:filePath atomically:YES];             }         }         else {             NSLog(@"%@", error);         }    }

(4) To refer to the file in the application documents directory

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);      NSString *documentsDirectory = [paths objectAtIndex:0];      NSError *jsonError = nil;      NSString *jsonFilePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"];      NSData *jsonData = [NSData dataWithContentsOfFile:jsonFilePath options:kNilOptions error:&jsonError ];