01.
// 将URL编码
02.
- (NSString *)encodeToPercentEscapeString: (NSString *) input
03.
{
04.
NSString *outputStr = (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
05.
(CFStringRef)input,
06.
NULL,
07.
(CFStringRef)@
"!*'();:@&=+$,/?%#[]"
,
08.
kCFStringEncodingUTF8));
09.
return
outputStr;
10.
}
11.
12.
// 将URL解码
13.
- (NSString *)decodeFromPercentEscapeString: (NSString *) input
14.
{
15.
NSMutableString *outputStr = [NSMutableString stringWithString:input];
16.
[outputStr replaceOccurrencesOfString:@
"+"
17.
withString:@
" "
18.
options:NSLiteralSearch
19.
range:NSMakeRange(
0
, [outputStr length])];
20.
21.
return
[outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
22.
}
演示一下上面的封装函数:
01.
NSString * testUrl = @
";a href="
" target="
_blank
" class="
keylink
">word</a>s=($# it's {a*123})00!*'();:@&=+$,/?%#[]"
;
02.
NSLog(@
"original: %@"
, testUrl);
03.
04.
05.
NSString * encodeStr = [self encodeToPercentEscapeString:testUrl];
06.
NSLog(@
"encoded: %@"
, encodeStr);
07.
08.
NSString * encodeStr2 = [testUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
09.
NSLog(@
"encoded2:%@"
, encodeStr2);
10.
11.
NSString * decodeStr = [self decodeFromPercentEscapeString:encodeStr];
12.
NSLog(@
"decoded: %@"
, decodeStr);
结果如下:
>> original: http://search.google.com?keys=($# it's {a*123})00!*'();:@&=+$,/?%#[]
>> encoded: http%3A%2F%2Fsearch.google.com%3Fkeywords%3D%28%24%23%20it%27s%20%7Ba%2A123%7D%2900%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5D >> encoded2: http://search.google.com?keywords=($%23%20it's%20%7Ba*123%7D)00!*'();:@&=+$,/?%25%23%5B%5D >> decoded: http://search.google.com?keywords=($# it's {a*123})00!*'();:@&=+$,/?%#[]
演示一下自带的URL转码测试代码:
01.
NSString* string1 = @
"文件夹"
;
02.
03.
NSString* string2 = [string1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
04.
05.
NSString* string3 = [string2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
06.
07.
NSString* string4 = [string2 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
08.
09.
NSString* string5 = [string3 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
10.
11.
NSString* string6 = [string4 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
12.
13.
NSString* string7 = [string5 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
14.
15.
16.
NSLog(@
"原始数据%@"
,string1);
17.
NSLog(@
"一层编码%@"
,string2);
18.
NSLog(@
"两层编码%@"
,string3);
19.
NSLog(@
"一层编码的一层解码%@"
,string4);
20.
NSLog(@
"两层编码的一层解码%@"
,string5);
21.
NSLog(@
"一层编码的两层解码%@"
,string6);
22.
NSLog(@
"两层编码的两层解码%@"
,string7);
打印结果:
2014-06-10 15:00:02.425 DareWayApp[7400:671651] 原始数据https://www.cloudsafe.com/文件夹
2014-06-10 15:00:02.426 DareWayApp[7400:671651] 一层编码https://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9
2014-06-10 15:00:02.427 DareWayApp[7400:671651] 两层编码https://www.cloudsafe.com/%25E6%2596%2587%25E4%25BB%25B6%25E5%25A4%25B9
2014-06-10 15:00:02.427 DareWayApp[7400:671651] 一层编码的一层解码https://www.cloudsafe.com/文件夹
2014-06-10 15:00:02.427 DareWayApp[7400:671651] 两层编码的一层解码https://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9
2014-06-10 15:00:02.427 DareWayApp[7400:671651] 一层编码的两层解码https://www.cloudsafe.com/文件夹
2014-06-10 15:00:02.427 DareWayApp[7400:671651] 两层编码的两层解码https://www.cloudsafe.com/文件夹
如果服务器用的是GBK编码,只要把上面的UTF改成下面的encoding就可以了:
1.
NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
2.
urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:gbkEncoding];
版权声明:本文为博主原创文章,未经博主允许不得转载。