13

我正在尝试打开以下网址,UIWebView但在将其更改为时无法加载:

 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\"}"]]]];

更新:

我故意转义了双引号,否则它会给我一个错误。我通过在我的浏览器中打开(在笔记本电脑上)检查了 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}
4

2 回答 2

21

您的代码行看起来很复杂,但基本上它是一个非常简单的代码。

您应该将此代码从单行拆分为更具可读性的多行。这也将允许您记录并检查您实际创建的 URL,如下所示:

NSLog(@"My url: %@", urlString);

更新:我看到你添加了完整的网址。Webview 确实无法加载该 url(UIWebkit 错误 101)。

导致问题的 url 部分是参数中的“#”字符和字典。您应该对 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,%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);
}
于 2012-10-08T19:53:57.237 回答
1

我会尝试对您的网址中的所有键/值项进行编码。特别是花括号 ({}) 和井号 (#) 符号可能会导致问题。

于 2012-10-08T19:37:47.443 回答