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).
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.
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.
It gives the following Error Log
in UIWebView delegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { }
Error Code : 101 & Description : Error Domain=WebKitErrorDomain Code=101 "The operation couldn’t be completed. (WebKitErrorDomain error 101.)" UserInfo=0x6e4cf60 {}
The most straightforward way is to use:
iDhaval was close, but he was doing it the other way around (decoding instead of encoding).
Anand's way would work, but you'll most likely have to replace more characters than spaces and new lines. See the reference here: http://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters
Hope that helps.
It's very simple to encode the URL in iPhone. It is as following
It's a perfect way to encode the URL, I am using it and it's perfectly work with me.
Hope it will help you!!!
The answer @Dhaval Vaishnani provided is only partially correct. This method treats the
?
,=
and&
characters as not to be encoded, since they're valid in an URL. Thus, to encode an arbitrary string to be safely used as a part of an URL, you can't use this method. Instead you have to fall back to using CoreFoundation andCFURLRef
:Don't forget to dispose of the ownership of the resulting string using
CFRelease(safeString);
.Also, it seems that despite the title, OP is looking for decoding and not encoding a string.
CFURLRef
has another, similar function call to be used for that:Again, don't forget proper memory management.
You can try this
You probably need to break the URL down into it's constituent parts and then URL encode the host and path but not the scheme. Then put it back together again.
Create an NSURL with the string and then use the methods on it such as host, scheme, path, query, etc to pull it apart. Then use CFURLCreateStringByAddingPercentEscapes to encode the parts and then you can put them back together again into a new NSURL.
I did some tests and I think the problem is not really with the
UIWebView
but instead thatNSURL
won't accept the URL because of the é in "Témp" is not encoded properly. This will cause+[NSURLRequest requestWithURL:]
and-[NSURL URLWithString:]
to returnnil
as the string contains a malformed URL. I guess that you then end up using anil
request with-[UIViewWeb loadRequest:]
which is no good.Example:
Output:
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: