@interface NSString (stringByDecodingURLFormat) - (NSString *)stringByDecodingURLFormat; - (NSString*)stringByURLEncodingStringParameter; @end
@implementation NSString(stringByDecodingURLFormat) - (NSString *)stringByDecodingURLFormat { NSString *result = [(NSString *)self stringByReplacingOccurrencesOfString:@"+" withString:@" "]; result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; return result; } //encode - (NSString*)stringByURLEncodingStringParameter { NSString *resultStr = self; CFStringRef originalString = (__bridge CFStringRef) self; CFStringRef leaveUnescaped = CFSTR(" "); CFStringRef forceEscaped = CFSTR("!*'();:@&=+$,/?%#[]"); CFStringRef escapedStr; escapedStr = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, originalString, leaveUnescaped, forceEscaped, kCFStringEncodingUTF8); if( escapedStr ) { NSMutableString *mutableStr = [NSMutableString stringWithString:(__bridge NSString *)escapedStr]; CFRelease(escapedStr); [mutableStr replaceOccurrencesOfString:@" " withString:@"%20" options:0 range:NSMakeRange(0, [mutableStr length])]; resultStr = mutableStr; } return resultStr; } @end
把以上代码假如到NSString 的分类就可以直接使用