xcode 如何将 JSON 响应保存到可从 UIWebWiew 中加载的本地 HTML 文件中访问的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9446503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:26:27  来源:igfitidea点击:

How can I save a JSON response to a file that would be accessible from within a local HTML file loaded inside UIWebWiew

iosxcodeios5

提问by Dustin Carpenter

I am receiving a JSON response and am currenlty able to make use of the data within my app.

我收到了一个 JSON 响应,并且目前可以使用我的应用程序中的数据。

I would like to save this response to a file so I could reference within an JS file located inside my project. I have already requested this data once when the application is launched so why not save it to a file and reference that throughout so only one call is needed for the data.

我想将此响应保存到文件中,以便我可以在位于我的项目内的 JS 文件中进行引用。我已经在应用程序启动时请求过一次这些数据,所以为什么不将它保存到一个文件中并在整个过程中引用它,这样数据只需要一次调用。

The HTML files for my UIWebView are imported into my Xcode project using the "create folder reference" option and the path to my JS file is html->js->app.js

我的 UIWebView 的 HTML 文件使用“创建文件夹引用”选项导入到我的 Xcode 项目中,我的 JS 文件的路径是 html->js->app.js

I want to save the response as data.jsonsomewhere on the device and then reference inside my js file like this request.open('GET', 'file-path-to-saved-json.data-file', false);

我想将响应保存在data.json设备上的某个地方,然后像这样在我的 js 文件中引用request.open('GET', 'file-path-to-saved-json.data-file', false);

How can I achieve this?

我怎样才能做到这一点?

回答by Dustin Carpenter

After working with the idea some more here is what I came up with.

在研究了这个想法之后,我想到了更多。

When the application is installed there is a default data file in the package that I copy to the Documents folder. When the application didFinishLaunchingWithOptionsruns I call the following method:

安装应用程序后,包中有一个默认数据文件,我将其复制到 Documents 文件夹中。当应用程序didFinishLaunchingWithOptions运行时,我调用以下方法:

- (void)writeJsonToFile
{   
//applications Documents dirctory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//live json data url
NSString *stringURL = @"http://path-to-live-file.json";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];

    //attempt to download live data
    if (urlData)
    {   
        NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"];
        [urlData writeToFile:filePath atomically:YES];
    }
    //copy data from initial package into the applications Documents folder
    else
    {
        //file to write to
        NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"];

        //file to copy from
        NSString *json = [ [NSBundle mainBundle] pathForResource:@"data" ofType:@"json" inDirectory:@"html/data" ];
        NSData *jsonData = [NSData dataWithContentsOfFile:json options:kNilOptions error:nil];

        //write file to device
        [jsonData writeToFile:filePath atomically:YES];
    }
}

Then throughout the application when I need to reference the data, I use the saved file.

然后在整个应用程序中,当我需要引用数据时,我会使用保存的文件。

//application Documents dirctory path
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 ];

To reference the json file in my JS code, I added a URL parameter for "src" and passed the file path to the Applications Documents folder.

为了在我的 JS 代码中引用 json 文件,我为“src”添加了一个 URL 参数,并将文件路径传递给 Applications Documents 文件夹。

 request.open('GET', src, false);