问题描述
我无法在 UIWebView
中打开一个网址,所以我已经进入了&发现我需要对URL进行编码,所以我尝试对其进行编码,但是我在URL编码方面遇到了问题:我的网址是http://somedomain.com/data/Témp%20Page%20- %20Open.html
(这不是真正的网址)。
I'm unable to open a URL into UIWebView
so I've seached & found that I need to encode URL, so I tried to encode it but, I've facing problem in URL encoding : My URL is http://somedomain.com/data/Témp%20Page%20-%20Open.html
(It's not real URL).
我关注%20
我试图用 stringByReplacingOccuranceOfString替换:@withString:@
,它给我一个我想要的URL http:/ /somedomain.com/data/TémpPage - Open.html
但它不能在 UIWebView
中打开,但令人惊讶的是它在中打开Safari
& FireFox
完美。即使我打开未编码的URL,它也会自动转换并打开我正在寻找的页面。
I'm concerned with %20
that I tried to replace using stringByReplacingOccuranceOfString:@"" withString:@""
, it give me the URL I wanted like http://somedomain.com/data/Témp Page - Open.html
However its not opening in UIWebView
but amazingly it opens in Safari
& FireFox
perfect. Even I open unencoded URL its automatically converts and open the page I'm looking for.
我已经google for URL encoding&它指出了我已经检查过的不同结果,但没有结果帮助我!我在不同的URL编码问题中尝试了不同的函数答案,但它只更改了所有特殊字符并使我的URL像 http%3A%2F%2Fsomedomain.com%2Fdata%2FT ...
无法在 UIWebView
中打开,甚至无法在任何浏览器中打开。
I've google for URL encoding & it points me to different results I already checked but no results help me out!! I tried different functions answers in different URL encoding question but it just changed all special characters and make my URL like, http%3A%2F%2Fsomedomain.com%2Fdata%2FT...
which can't open in UIWebView
and even in any browser.
它提供以下错误日志
in UIWebView委托
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { }
推荐答案
我做了一些测试,我认为问题不是真的与 UIWebView
,而是 NSURL
将不接受该URL,因为Témp中的é编码不正确。这将导致 + [NSURLRequest requestWithURL:]
和 - [NSURL URLWithString:]
返回 nil
因为字符串包含格式错误的URL。我猜你最后用 - [UIViewWeb loadRequest:]
使用 nil
请求,这是不行的。
I did some tests and I think the problem is not really with the UIWebView
but instead that NSURL
won't accept the URL because of the é in "Témp" is not encoded properly. This will cause +[NSURLRequest requestWithURL:]
and -[NSURL URLWithString:]
to return nil
as the string contains a malformed URL. I guess that you then end up using a nil
request with -[UIViewWeb loadRequest:]
which is no good.
示例:
NSLog(@"URL with é: %@", [NSURL URLWithString:@"http://host/Témp"]);
NSLog(@"URL with encoded é: %@", [NSURL URLWithString:@"http://host/T%C3%A9mp"]);
输出:
2012-10-02 12:02:56.366 test[73164:c07] URL with é: (null)
2012-10-02 12:02:56.368 test[73164:c07] URL with encoded é: http://host/T%C3%A9mp
如果你真的想借优雅地处理WebKit所具有的格式错误的URL并且不想自己实现它你可以做类似的事情,但它非常难看:
If you really really want to borrow the graceful handling of malformed URLs that WebKit has and don't want to implement it yourself you can do something like this but it is very ugly:
UIWebView *webView = [[[UIWebView alloc]
initWithFrame:self.view.frame]
autorelease];
NSString *url = @"http://www.httpdump.com/texis/browserinfo/Témp.html";
[webView loadHTMLString:[NSString stringWithFormat:
@"<script>window.location=%@;</script>",
[[[NSString alloc]
initWithData:[NSJSONSerialization
dataWithJSONObject:url
options:NSJSONReadingAllowFragments
error:NULL]
encoding:NSUTF8StringEncoding]
autorelease]]
baseURL:nil];
这篇关于iOS:如何进行正确的URL编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!