【问题标题】:Is it possible to use an Xcode build script to download JSON files to the app bundle?是否可以使用 Xcode 构建脚本将 JSON 文件下载到应用程序包?
【发布时间】:2014-09-06 16:36:23
【问题描述】:

我有一个本地运行的 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

【问题讨论】:

  • 一篇写得很好的帖子,但事实上,你的问题太宽泛了,可能的解决方案太多了。 Stack Overflow 旨在帮助修复损坏的代码。如果您可以发布一些您自己的 bash 代码,您会做得最好,然后我们可以帮助您修复它。而且......是的,这对于您在问题上标记的工具来说当然是可行的。祝你好运
  • 不要将任何数据保存到应用程序包中,因为系统会在启动时测试应用程序校验和。保存到 Documents 文件夹。

标签: ios json xcode bash shell


【解决方案1】:

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

我去了我的目标 -> 构建阶段 -> + -> '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"

我目前正在为流程添加一些验证,但作为一个起点,这对我有用。

希望有帮助!

更新

以下是添加了一些 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

【讨论】:

    【解决方案2】:

    不确定我是否在这里获得了您的规范,但这就是我已经实施的以及此解决方案的一些帮助:How can I save a JSON response to a file that would be accessible from within a local HTML file loaded 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);
             }
    
        }
    

    (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 ];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      相关资源
      最近更新 更多