Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURL with special characters

How can I encode this url to be displayed in a UIWebview:

http://de.wikipedia.org/?search=Bevölkerungsentwicklung

I tried:

-stringByAddingPercentEscapesUsingEncoding:NSUnicodeStringEncoding
-stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding

and

CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                        (CFStringRef)mobileUrl,
                                        NULL,
                                        (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                        kCFStringEncodingUTF8);

thanks

joerg

like image 648
Joerg Avatar asked Nov 10 '10 15:11

Joerg


1 Answers

Encode just the search part of the URL string:

// searchString is the unescaped search string, e.g., "Bevölkerungsentwicklung"

NSString *encodedSearchString = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:@"http://de.wikipedia.org/?search=%@", encodedSearchString];
NSURL *url = [NSURL URLWithString:urlString];

(Note as well that NSUTF8StringEncoding is the encoding used.)

like image 112
Wevah Avatar answered Nov 09 '22 14:11

Wevah