是否可以使用Xcode构建脚本将JSON文件下载到应用程序包?

And*_*ula 7 bash shell xcode json ios

我有一个本地运行的Web服务器,它从许多端点提供JSON格式的数据.我目前将每个端点的数据包含在单独的.json文件中,我手动将其添加到应用程序包中以便在应用程序中使用.是否可以在构建项目时自动执行此过程,可能使用Xcode构建脚本?

下面是我想要实现的一个例子.

  1. 从localhost:3000/example获取JSON格式的数据.
  2. 如果无法到达端点,请在此处停止.
  3. 将数据保存在名为example.json的文件中.
  4. 将example.json添加到应用程序包以在应用程序中使用.

任何有关这方面的帮助将不胜感激.

编辑

我已经获取了JSON格式的数据,但我现在正在寻找如何将这些数据复制到应用程序包中.

curl -o example.json http://localhost:3000/example
Run Code Online (Sandbox Code Playgroud)

pof*_*off 6

这就是我在项目中做同样事情的方式。

我进入了目标->构建阶段-> +->'New Run Script Phase',并添加了以下脚本:

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"
Run Code Online (Sandbox Code Playgroud)

我目前正在为流程添加一些验证,但是作为起点,这对我来说是有效的。

希望有帮助!

更新

这是添加了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
Run Code Online (Sandbox Code Playgroud)


小智 0

不确定我是否在这里得到了您的规范,但这就是我已经实现的以及此解决方案的一些帮助:How can I save a JSON response to a file that will beaccessible from inside a local HTML file returned inside UIWebWiew

(1) 这是抓取 json 文件的函数。该函数基本上是将您要定位的 url 分配给一个字符串。然后将其格式化为 NSURL。然后将其格式化为 NSData,它将抓取 url 的内容。数据将被序列化,然后准备好定位适当的元素。

    - (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);
         }

    }
Run Code Online (Sandbox Code Playgroud)

(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 ];
Run Code Online (Sandbox Code Playgroud)