Discover the NSString class has built-in methods to implement. They were:
-(NSString *) stringbyaddingpercentescapesusingencoding: (nsstringencoding) encoding
-(NSString *) stringbyreplacingpercentescapesusingencoding: (nsstringencoding) encoding
As long as the corresponding encoding can be encoded and decoded, but this method is to encode the entire URL, so if there is a query string with &?/, etc. will not be encoded conversion, if there is a need for this friend can refer to the URL provided below, Find a solution: http://www.cocoachina.com/bbs/read.php?tid-2469.html
Example:
nsstring* string1 = @"https://www.cloudsafe.com/folder";
nsstring* string2 = [string1 stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
nsstring* string3 = [string2 stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
nsstring* String4 = [string2 stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
nsstring* string5 = [String3 stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
nsstring* String6 = [String4 stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
nsstring* String7 = [String5 stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
Output: String5 & 6 Repeat code encode
String1:https://www.cloudsafe.com/folder
String2:https://www.cloudsafe.com/%e6%96%87%e4%bb%b6%e5%a4%b9
String3:https://www.cloudsafe.com/%25e6%2596%2587%25e4%25bb%25b6%25e5%25a4%25b9
String4:https://www.cloudsafe.com/folder
String5:https://www.cloudsafe.com/%e6%96%87%e4%bb%b6%e5%a4%b9
String6:https://www.cloudsafe.com/folder
String7:https://www.cloudsafe.com/folder