非ARC模式下:
- (NSString *)encodeToPercentEscapeString: (NSString *) input
{
NSString *outputStr = (NSString *)
CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)input,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
return outputStr;
} - (NSString *)decodeFromPercentEscapeString: (NSString *) input
{
NSMutableString *outputStr = [NSMutableString stringWithString:input];
[outputStr replaceOccurrencesOfString:@"+"
withString:@" "
options:NSLiteralSearch
range:NSMakeRange(, [outputStr length])]; return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
ARC模式下:
- (NSString *)encodeToPercentEscapeString: (NSString *) input
{
NSString* outputStr = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL, /* allocator */
(__bridge CFStringRef)input,
NULL, /*解决含有的转义字符*/
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
return outputStr;
}
- (NSString *)decodeFromPercentEscapeString: (NSString *) input
{
NSMutableString *outputStr = [NSMutableString stringWithString:input];
[outputStr replaceOccurrencesOfString:@"+"
withString:@""
options:NSLiteralSearch
range:NSMakeRange(,
[outputStr length])]; return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}