NSString *strUTF8 = @"太平洋保险(太保)人保,平保,人寿";
//urlencode :
NSString *strUrl = [strUTF8 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//urldecode 解析网址上中文时用到
NSString *strUrlDecode = [strUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//在线工具
http://tool.chinaz.com/Tools/URLEncode.aspx
自己打开网址时,带中文参数时,需要编码
- (NSString*)URLencode:(NSString *)originalString
stringEncoding:(NSStringEncoding)stringEncoding {
//! @ $ & ( ) = + ~ ` ; ' : , / ?
//%21%40%24%26%28%29%3D%2B%7E%60%3B%27%3A%2C%2F%3F
NSArray *escapeChars = [NSArray arrayWithObjects:@";" , @"/" , @"?" , @":" ,
@"@" , @"&" , @"=" , @"+" , @"$" , @"," ,
@"!", @"'", @"(", @")", @"*", nil];
NSArray *replaceChars = [NSArray arrayWithObjects:@"%3B" , @"%2F", @"%3F" , @"%3A" ,
@"%40" , @"%26" , @"%3D" , @"%2B" , @"%24" , @"%2C" ,
@"%21", @"%27", @"%28", @"%29", @"%2A", nil];
int len = [escapeChars count];
NSMutableString *temp = [[originalString
stringByAddingPercentEscapesUsingEncoding:stringEncoding]
mutableCopy];
int i;
for (i = 0; i < len; i++) {
[temp replaceOccurrencesOfString:[escapeChars objectAtIndex:i]
withString:[replaceChars objectAtIndex:i]
options:NSLiteralSearch
range:NSMakeRange(0, [temp length])];
}
NSString *outStr = [NSString stringWithString: temp];
NSLog(@"%@",outStr);
return outStr;
}
同样打开中文网址,需要解析参数时,也需要解码
NSString *strZW = ...//中文参数
NSString* stringDe = [strZW stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
来源:oschina
链接:https://my.oschina.net/u/877519/blog/201067