URL encode 用于将字符串以 URL 编码,具体代码如下:
+(NSString *)encodeString:(NSString *)unencodeString
{
NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)unencodeString, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8));
return unencodeString;
}
-(NSString *)decodeString:(NSString *)encodedString
{
NSString * decodedString = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)encodedString, CFSTR(""),CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
return encodedString;
}
当然也可以不写方法体,直接复制方法内部的代码,把相关变量替换即可。
附: URL在线解析: [ UrlEncode编码/UrlDecode解码 - 站长工具 ]
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:URL encode编码和解码