iOS url编码解码

编码 URLEncodedString

-(NSString *)URLEncodedString:(NSString *)str  
{  
    NSString *encodedString = (NSString *)  
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,  
                                                              (CFStringRef)str,  
                                                              NULL,  
                                                              (CFStringRef)@"!*'();:@&=+$,/?%#[]",  
                                                              kCFStringEncodingUTF8));  
      
    return encodedString;  
} 

解码 URLDecodedString

-(NSString *)URLDecodedString:(NSString *)str  
{  
    NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));  
      
    return decodedString;  
}

调用方法

NSString *readyEcodeStr=@"adsfa asdfa s+asdf";//准备urlecode的字符串  
    NSString *ecodeStr=[self URLEncodedString:readyEcodeStr];  
    NSLog(@"readyEcodeStr=%@  ecodeStr=%@",readyEcodeStr,ecodeStr);  

打印出来的log如下:
readyEcodeStr=adsfa asdfa s+asdf ecodeStr=adsfa%20asdfa%20s%2Basdf
原文:
http://blog.csdn.net/lwjok2007/article/details/47023853

你可能感兴趣的:(iOS url编码解码)