将参数发送到UIWebView中的本地文件 [英] Send parameters to a local file in a UIWebView

查看:69
本文介绍了将参数发送到UIWebView中的本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发原生iPhone应用程序。我能够将本地文件index.html加载到UIWebView中:

I'm working on a native iPhone app. I am able to load a local file index.html into a UIWebView:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

网络加载正常。现在,我想用一些参数加载网页,就像在浏览器中输入一样:
URL?var1 = myValue& var2 = myValue2

The web loads fine. Now, I would like to load the web with some parameters, in the same way one types this in the browser: URL?var1=myValue&var2=myValue2

所以我可以在html文件的javascript中获取这些值。有没有办法将参数发送到本地html文件?

So I can get those values inside the javascript of the html file. Is there any way to send parameters to a local html file?

谢谢!

推荐答案

最好的和真正有效的答案,只在objC中(记得保留你的文件夹结构 - 使用为任何添加的文件夹创建文件夹参考将文件复制到xcode中)

The best and really working answer, only in objC (remember to retain your folder structure - copy files into xcode using "Create Folder References for any added folders")

NSString *path = [[NSBundle mainBundle]
       pathForResource:@"YOUR-HTML-FILE"
       ofType:@"html"
       inDirectory:@"YOUR-FOLDER" ]; 
 NSURL *url = [NSURL fileURLWithPath:path];



 NSString *theAbsoluteURLString = [url absoluteString];   

 NSString *queryString = @"?var=1232123232"; 

 NSString *absoluteURLwithQueryString = [theAbsoluteURLString stringByAppendingString: queryString];  

 NSURL *finalURL = [NSURL URLWithString: absoluteURLwithQueryString];

NSURLRequest *request = [NSURLRequest requestWithURL:finalURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];

[web loadRequest:request];

这篇关于将参数发送到UIWebView中的本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆