IOS Solution URL Chinese garbled problem solving
When making an HTTPS connection, the client is to synthesize an HTTPS address
If the address contains Chinese, the program will be crash, check found that the original is not the reason for the Chinese transcoding
The following two methods were found inside the NSString library
-(NSString *) stringbyaddingpercentescapesusingencoding: (nsstringencoding) encoding
-(NSString *) Stringbyreplacingpercentescapesusingencoding: (nsstringencoding) encoding
Try it, it works.
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 string1-7 results are as follows
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
Simply say how I use it:
After the URL is synthesized, give the entire string two times
nsmutablestring *address = [[Nsmutablestring Stringwithstring:setnichengstringwithoutusernameandnicheng] Mutablecopy];
address = [[Address stringByAppendingString:app.name] mutablecopy];
address = [[Address stringbyappendingstring:@ '/'] mutablecopy];
address = [[Address Stringbyappendingstring:_nametextfield.text] mutablecopy];
address = [[Address stringbyaddingpercentescapesusingencoding:nsutf8stringencoding] mutablecopy];
address = [[Address stringbyaddingpercentescapesusingencoding:nsutf8stringencoding] mutablecopy];
How the server side is simply stored, the server can receive data without transcoding into Chinese
When the client requests this part of the data, it is better to decode by the client itself.
After getting to the string, the decoding can be displayed normally as Chinese:
str = [STR stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
Thank you for reading, I hope to help you, thank you for your support for this site!