[英]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. 我有一个本地运行的Web服务器,它从许多端点提供JSON格式的数据。 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.
我目前将每个端点的数据包含在单独的.json文件中,我手动将其添加到应用程序包中以便在应用程序中使用。 Is it possible to automate this process whenever the project is built, perhaps using an Xcode build script?
是否可以在构建项目时自动执行此过程,可能使用Xcode构建脚本?
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. 我已经获取了JSON格式的数据,但我现在正在寻找如何将这些数据复制到应用程序包中。
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 UPDATE
Here is the same code with some added JSON validation: 以下是一些添加了JSON验证的相同代码:
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
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 不确定我是否在这里获得您的规范,但这是我已实现的以及此解决方案的一些帮助: 如何将JSON响应保存到可从UIWebWiew中加载的本地HTML文件中访问的文件中
(1) Here is the function that will grab the json file. (1)这是将获取json文件的函数。 This function is basically assigning the url you are targeting to a string.
此功能基本上是将您定位的网址分配给字符串。 Then formatting it to a NSURL.
然后将其格式化为NSURL。 This is then formatted as NSData which will grab the contents of the url.
然后将其格式化为NSData,它将获取URL的内容。 The data will be serialised and then 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 (4)引用应用程序文档目录中的文件
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 ];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.