NSURL with special characters
How can I encode this url to be displayed in a UIWebview:
http://de.wikipedia.org/?search=Bevölkerungsentw开发者_如何学运维icklung
I tried:
-stringByAddingPercentEscapesUsingEncoding:NSUnicodeStringEncoding
-stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding
and
CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)mobileUrl,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
thanks
joerg
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.)
Just use below sample code;
NSString *urlstring = [NSString stringWithFormat:@"http://de.wikipedia.org/?search=%@", searchString];
NSString *encodedString = [urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:encodedString];
I found also that for some North European characters, NSISOLatin1StringEncoding fits better. This one gives me a better result
- (void) testEncoding {
NSString * urlString = @"http://example/path/fileName_blå.pdf";
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
NSURL * url = [NSURL URLWithString:urlString];
NSLog(@"URL: %@", url);
}
精彩评论