iOS:WebView加载网址 [英] iOS: WebView Loading a url
问题描述
我正在尝试在 UIWebView
中打开以下网址,但无法加载,而是将其更改为:
I am trying to open the following url in UIWebView
but it fails to load whereas changing it to:
http://www.google.com
工作正常。
我要加载的网址是:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@%@%@",@"http://m.forrent.com/search.php?address=",[[bookListing objectForKey:@"Data"] objectForKey:@"zip"],@"&beds=&baths=&price_to=0#{\"lat\":\"0\",\"lon\":\"0\",\"distance\":\"25\",\"seed\":\"1622727896\",\"is_sort_default\":\"1\",\"sort_by\":\"\",\"page\":\"1\",\"startIndex\":\"0\",\"address\":\"",[[bookListing objectForKey:@"Data"] objectForKey:@"zip"],@"\",\"beds\":\"\",\"baths\":\"\",\"price_to\":\"0\"}"]]]];
更新:
我故意逃脱双引号,否则它给我一个错误。
我通过在浏览器中打开(在笔记本电脑上)检查了网址,它完全正常:
I have purposely escaped the double quotes otherwise it gives me an error. I checked the url by opening in my browser (on laptop) and it works perfectly fine:
浏览器中的网址:
http://m.forrent.com/search.php?address=92115&beds=&baths=&price_to=0#{%22lat%22:%220%22,%22lon%22:%220%22,%22distance%22:%2225%22,%22seed%22:%221622727896%22,%22is_sort_default%22:%221%22,%22sort_by%22:%22%22,%22page%22:%221%22,%22startIndex%22:%220%22,%22address%22:%2292115%22,%22beds%22:%22%22,%22baths%22:%22%22,%22price_to%22:%220%22}
推荐答案
您的代码行看起来很复杂,但基本上它是一个非常简单的代码。
Your line of code looks convoluted, but basically it is a very simple one.
您应该将此代码从一行分解为多行更具可读性。
这也将允许您记录并检查您实际创建的URL,如下所示:
You should breakup this code from a one liner to multiple lines that are more readable. That will also allow you to log and check the URL you actually created, like so:
NSLog(@我的网址: %@,urlString);
更新:
我看到你添加了完整的网址。 Webview确实无法加载该URL(UIWebkit错误101)。
Update: I see you added the full url. Webview indeed fails to load that url (UIWebkit error 101).
导致问题的url部分是params后面的'#'字符和字典。你应该url编码网址的那一部分。
The part of the url that causes the problem is the '#' character and dictionary that follows in the params. You should url encode that part of the url.
试试这个:
NSString *address = @"http://m.forrent.com/search.php?";
NSString *params1 = @"address=92115&beds=&baths=&price_to=0";
// URL encode the problematic part of the url.
NSString *params2 = @"#{%22lat%22:%220%22,%22lon%22:%220%22,%22distance%22:%2225%22,%22seed%22:%221622727896%22,%22is_sort_default%22:%221%22,%22sort_by%22:%22%22,%22page%22:%221%22,%22startIndex%22:%220%22,%22address%22:%2292115%22,%22beds%22:%22%22,%22baths%22:%22%22,%22price_to%22:%220%22}";
params2 = [self escape:params2];
// Build the url and loadRequest
NSString *urlString = [NSString stringWithFormat:@"%@%@%@",address,params1,params2];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
我使用的转义方法:
- (NSString *)escape:(NSString *)text
{
return (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef)text, NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
}
这篇关于iOS:WebView加载网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!