IOS URLEcode, URLDecode
IOS requires an httpurl interaction with the server when it involves network programming.
If the url contains Chinese characters or the image needs to be uploaded, you must consider urlEncode (otherwise, the url is incorrect)
Some messages returned by the server may also have the same problem. In this case, the server may return urlEcode, and the client needs to decode urlDecode.
In fact, the method is very simple. You only need to create the following two methods:
Encode URLEncodedString
-(NSString *)URLEncodedString:(NSString *)str{ NSString *encodedString = (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, (CFStringRef)@!*'();:@&=+$,/?%#[], kCFStringEncodingUTF8)); return encodedString;}
Decode URLDecodedString
-(NSString *)URLDecodedString:(NSString *)str{ NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)); return decodedString;}
Call method:
NSString * readyEcodeStr = @ adsfa asdfa s + asdf; // The NSString * ecodeStr = [self URLEncodedString: readyEcodeStr]; NSLog (@ readyEcodeStr = % @ ecodeStr = % @, readyEcodeStr, ecodeStr );
The printed log is as follows:
ReadyEcodeStr = adsfa asdfa s + asdf
EcodeStr = adsfa % 20 asdfa % 20 s % 2 Basdf
OK is basically implemented