使用javascript从文档文件夹iOS读取JSON文件
在我的应用程序中,我需要读取存储在iPhone文档文件夹中的JSON文件。我看到存在一种连接javascript和objective C的方法。我如何做到这一点?我知道我应该在objective C使用javascript从文档文件夹iOS读取JSON文件,javascript,iphone,json,directory,Javascript,Iphone,Json,Directory,在我的应用程序中,我需要读取存储在iPhone文档文件夹中的JSON文件。我看到存在一种连接javascript和objective C的方法。我如何做到这一点?我知道我应该在objective C-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType中使用此方法,但我应该用
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType中使用此方法代码>,但我应该用javascript写什么?我发现了这个:,但我不明白它是如何工作的。在我的应用程序中,我必须执行以下操作:
从移动站点获得报价(我制作了一些小型移动站点)
我将移动站点本地存储在iPhone的Documents文件夹中
我生成了一个JSON文件,用于保存报价的名称、路径和过期日期。我将此JSON存储在iPhone的Documents文件夹中
我的应用程序有一个“归档”视图控制器,我在其中加载一个移动站点,在其中我将通过读取创建的JSON来列出存储在设备中的所有服务
我制作了本机应用程序,它可以工作,现在我必须实现从Documents文件夹读取JSON文件的方法。有人能帮我做这个操作吗?如何从javascript读取JSON,解析它并在html动态列表中显示结果
多谢各位
代码更新:目前我编写了以下代码:
目标C代码片段
- (void)parseJson {
// Carico il documento JSON dalla cartella documents
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.json"];
BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (fileExist) {
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dictContent = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
if (dictContent) {
NSDictionary *sites = [dictContent objectForKey:@"sites"];
NSArray *site = [sites objectForKey:@"site"];
NSMutableArray *names = [[NSMutableArray alloc]init];
NSMutableArray *srcs = [[NSMutableArray alloc]init];
for (int i = 0; i < [site count]; i++) {
NSDictionary *dictData = [site objectAtIndex:i];
[names addObject:[dictData objectForKey:@"name"]];
[srcs addObject:[dictData objectForKey:@"src"]];
};
NSString *javascriptString = [NSString stringWithFormat:@"dataFromObjC([%@, %@])", names, srcs];
[self.webView stringByEvaluatingJavaScriptFromString:javascriptString];
}
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Promo" message:@"Non hai ancora salvato nessuna promozione!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
}
// Faccio il parse del documento JSON
// Passo le informazioni parsate al javascript
}
我希望这将有助于解决这个问题。我使用了stringByEvaluatingJavaScriptFromString
,因为我在这里找到了一篇文章,可以将数据从objective C读取到javascript。因为您只需要读取JSON,然后通过以下方式读取:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"myJson.json"];
NSData *jsonData = [[NSFileManager defaultManager] contentsAtPath:fullPath];
然后将其解析到nsdictionary:
id jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
再次更新:
如果要从javascript调用obj c,需要定义一个特定的URL,如:
http://abc.com/?key=value
然后从js调用它:
function callObjC(){
location.href='http://abc.com/?key=value';
}
然后在shouldStartLoadWithRequest中,比较请求的url和特定的url,如果它们相同,则执行您的功能
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] absoluteString] hasPrefix:@"http://abc.com/"]) {
//comparing urls, if almost the same
//if you need to pass parameters to Obj C, put them in the URL as my example and explode yourself
[self anotherMethod];//your method
return NO;//skip the request, as the link is not valid or we don't want to change the webview
}
return YES;
}
您的应用程序使用自定义协议:js调用而不是http。
它可以工作,但苹果不推荐它。好的,我知道这一点。但我必须用javascript来做。。。我怎么能做到?你怎么看?
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] absoluteString] hasPrefix:@"http://abc.com/"]) {
//comparing urls, if almost the same
//if you need to pass parameters to Obj C, put them in the URL as my example and explode yourself
[self anotherMethod];//your method
return NO;//skip the request, as the link is not valid or we don't want to change the webview
}
return YES;
}