如何对此网址进行编码以显示在UIWebview中:
http://de.wikipedia.org/?search=Bevölkerungsentwicklung
我试过了:
-stringByAddingPercentEscapesUsingEncoding:NSUnicodeStringEncoding
-stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding
和
CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)mobileUrl,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
感谢
约尔格
答案 0 :(得分:7)
只对URL字符串的搜索部分进行编码:
// 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];
(另请注意,NSUTF8StringEncoding是使用的编码。)
答案 1 :(得分:2)
使用下面的示例代码;
NSString *urlstring = [NSString stringWithFormat:@"http://de.wikipedia.org/?search=%@", searchString];
NSString *encodedString = [urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:encodedString];
答案 2 :(得分:1)
我还发现,对于一些北欧角色,NSISOLatin1StringEncoding更合适。 这个给了我更好的结果
- (void) testEncoding {
NSString * urlString = @"http://example/path/fileName_blå.pdf";
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
NSURL * url = [NSURL URLWithString:urlString];
NSLog(@"URL: %@", url);
}