我试图在UIWebView中打开以下url,但是无法加载,而将其更改为:
http://www.google.com
工作正常.
我要加载的url是:
[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\":\"",@"\",\"beds\":\"\",\"baths\":\"\",\"price_to\":\"0\"}"]]]];
更新:
我有意逃避双引号,否则给我一个错误.
我通过在浏览器中打开浏览器(笔记本电脑)检查了URL,它的工作原理很好:
浏览器中的网址:
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}
解决方法
你的代码行很复杂,但基本上这是一个非常简单的代码.
你应该将这个代码从一个内存分解成多个可读的行.
这也将允许您记录并检查您实际创建的URL,如下所示:
NSLog(@“我的url:%@”,urlString);
更新:
我看到你添加了完整的URL. Webview确实无法加载该url(UIWebkit错误101).
导致问题的url的部分是params中的’#’字符和字典.你应该url编码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,%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); }