iOS:WebView加载网址

我试图在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);
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...