Hec*_*ret 11 iphone objective-c uiwebview ios
我正在使用原生iPhone应用程序.我能够将本地文件index.html 加载到UIWebView中:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
Run Code Online (Sandbox Code Playgroud)
网络加载很好.现在,我想用一些参数加载web,就像在浏览器中输入一样:URL?var1 = myValue&var2 = myValue2
所以我可以在html文件的javascript中获取这些值.有没有办法将参数发送到本地html文件?
谢谢!
小智 26
最好的和真正有效的答案,仅在objC中(记住要保留你的文件夹结构 - 使用"为任何添加的文件夹创建文件夹参考"将文件复制到xcode中)
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];
Run Code Online (Sandbox Code Playgroud)
马里安的方式是让它发挥作用的正确方法.
正如我的朋友Terence指出的那样,诀窍是在正确地从字符串创建url时使用绝对文件uri(file://)字符串.这条路 ?字符不会被编码,webview可以正确加载文件.
[NSURL URLWithString: @"file://localhost/Users/sj/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/../PrintingWebView.app/final.html?slide=2"];
Run Code Online (Sandbox Code Playgroud)
或者使用URLWithString:relativeToURL方法
NSURL *relativeURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSURL *u = [NSURL URLWithString:@"html/index.html?site=2" relativeToURL:relativeURL];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14040 次 |
| 最近记录: |