iOS:WebView 加载 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12787914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: WebView Loading a url
提问by iDev
I am trying to open the following url in UIWebView
but it fails to load whereas changing it to:
我正在尝试打开以下网址,UIWebView
但无法加载,而将其更改为:
http://www.google.com
works fine.
工作正常。
The url that I want to load is:
我要加载的网址是:
[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\"}"]]]];
UPDATE:
更新:
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:
我故意转义双引号,否则它会给我一个错误。我通过在浏览器中打开(在笔记本电脑上)检查了 url,它工作得很好:
The url in browser:
浏览器中的网址:
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}
回答by PostPCDev
Your line of code looks convoluted, but basically it is a very simple one.
您的代码行看起来很复杂,但基本上它是一个非常简单的代码。
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:
您应该将此代码从一行代码分解为更具可读性的多行代码。这也将允许您记录并检查您实际创建的 URL,如下所示:
NSLog(@"My url: %@", urlString);
NSLog(@"My url: %@", urlString);
Update: I see you added the full url. Webview indeed fails to load that url (UIWebkit error 101).
更新:我看到你添加了完整的 url。Webview 确实无法加载该 url(UIWebkit 错误 101)。
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.
导致问题的 url 部分是参数中的“#”字符和字典。您应该对 url 的那部分进行 url 编码。
Try this:
尝试这个:
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]]];
The escaping method I used:
我使用的转义方法:
- (NSString *)escape:(NSString *)text
{
return (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef)text, NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
}
回答by bgolson
I would try encoding all of the key/value items in your url. Specifically the curly braces ({}) and the hash (#) symbols may be causing a problem.
我会尝试对您 url 中的所有键/值项进行编码。特别是花括号 ({}) 和哈希 (#) 符号可能会导致问题。