Method 1:
nsstring* encodedstring = [urlstringstringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
Method 2:
NSString * encodedstring = (NSString *) cfurlcreatestringbyaddingpercentescapes (Kcfallocatordefault, (CFSTRINGREF) URLSTRING,NULL,NULL,KCFSTRINGENCODINGUTF8);
If this method is used in all classes, it can be written as category and then called in the header file import "Nsstring+url.h".
Nsstring+url.h file
@interface NSString (URL)-(NSString *) urlencodedstring; @end
NSSTRING+URL.M file
#import "Nsstring+url.h" @implementation nsstring (URL)-(NSString *) urlencodedstring{ nsstring *encodedstring = ( NSString *) cfurlcreatestringbyaddingpercentescapes (Kcfallocatordefault, (cfstringref) Self, ( CFSTRINGREF) @ "!$&" () *+,-./:; [Email protected]_~%#[] ", NULL, kCFStringEncodingUTF8); return encodedstring;} @end
Call Method:
NSString *encodedstring = [URLString urlencodedstring];
EncodedString do something
iOS Chinese URL path conversion urlencode