url编码、解码
1.url编码
ios中http请求遇到汉字的时候,需要转化成UTF-8,用到的方法是:
NSString * encodingString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
或者可以:
+ (NSString*)encodeToPercentEscapeString: (NSString*) input
{
NSString*outputStr = (NSString*)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)input,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
returnoutputStr;
}
2.url解码
请求后,返回的数据,如果显示的是这样的格式:%3A%2F%2F,此时需要我们进行UTF-8解码,用到的方法是:
NSString *str = [model.album_name stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];