iOS : How to do proper URL encoding? iOS : How to do proper URL encoding? ios ios

iOS : How to do proper URL encoding?


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 and CFURLRef:

NSString *unsafeString = @"this &string= confuses ? the InTeRwEbZ";CFStringRef safeString = CFURLCreateStringByAddingPercentEscapes (    NULL,    (CFStringRef)unsafeString,    NULL,    CFSTR("/%&=?$#+-~@<>|\\*,.()[]{}^!"),    kCFStringEncodingUTF8);

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:

NSString *escapedString = @"%32%65BCDEFGH";CFStringRef unescapedString = CFURLCreateStringByReplacingPercentEscapesUsingEncoding (    NULL,    (CFStringRef)escapedString,    CFSTR(""),    kCFStringEncodingUTF8);

Again, don't forget proper memory management.


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.

Example:

NSLog(@"URL with é: %@", [NSURL URLWithString:@"http://host/Témp"]);NSLog(@"URL with encoded é: %@", [NSURL URLWithString:@"http://host/T%C3%A9mp"]);

Output:

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

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];


The most straightforward way is to use:

NSString *encodedString = [rawString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

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.


matomo