@interface nsstring (Stringbydecodingurlformat)-(NSString *) Stringbydecodingurlformat; -(nsstring*) stringbyurlencodingstringparameter; @end
@implementationnsstring (Stringbydecodingurlformat)-(NSString *) stringbydecodingurlformat{NSString*result = [(NSString *) Self stringbyreplacingoccurrencesofstring:@"+"Withstring:@" "]; Result=[result stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding]; returnresult;}//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:0Range:nsmakerange (0, [mutablestr length])]; ResultStr=Mutablestr; } returnResultStr;}@end
The above code, if the classification of nsstring can be used directly
Objective-c UrlEncode UrlDecode