如何将JSON响应保存到可从UIWebWiew中加载的本地HTML文件中访问的文件 [英] How can I save a JSON response to a file that would be accessible from within a local HTML file loaded inside UIWebWiew
问题描述
我收到了JSON响应,并且当前能够使用我的应用程序中的数据。
I am receiving a JSON response and am currenlty able to make use of the data within my app.
我想将此响应保存到文件中,以便我可以在项目中的JS文件中引用。我已经在应用程序启动时请求了一次这样的数据,所以为什么不将它保存到文件中并引用它,因此只需要一次调用数据。
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.
我的UIWebView的HTML文件使用创建文件夹参考选项导入到我的Xcode项目中,我的JS文件的路径是 html-> js-> app.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
我想在设备的某个地方将响应保存为 data.json
然后在我的js文件中引用像这样 request.open('GET','file-path-to-saved-json.data-file',false);
I want to save the response as data.json
somewhere on the device and then reference inside my js file like this request.open('GET', 'file-path-to-saved-json.data-file', false);
我怎样才能实现这个目标?
How can I achieve this?
推荐答案
在完成这个想法之后,我来了更多使用。
After working with the idea some more here is what I came up with.
安装应用程序时,程序包中有一个默认数据文件,我将其复制到Documents文件夹。当应用程序 didFinishLaunchingWithOptions
运行时,我调用以下方法:
When the application is installed there is a default data file in the package that I copy to the Documents folder. When the application didFinishLaunchingWithOptions
runs I call the following method:
- (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 ];
为了在我的JS代码中引用json文件,我为src添加了一个URL参数并通过了应用程序文档文件夹的文件路径。
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.
request.open('GET', src, false);
这篇关于如何将JSON响应保存到可从UIWebWiew中加载的本地HTML文件中访问的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!