IOS URL Chinese garbled problem solution _ios

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.