From http://www.cnblogs.com/kiao295338444/articles/2590031.html
We know that the web page is downloaded using the nsurlconnection proxy method and saved to an nsdata file,
Nsmutabledata * pagedata;
[Pagedata appenddata: Data];
If the webpage code is UTF-8, it can be converted to a string like this:
Nsstring * pagesource = [[nsstring alloc] initwithdata: pagedata encoding: nsutf8stringencoding];
If the webpage is GBK (or gb2312) and utf8 is used for conversion, pagesource returns nil. In this case, GBK encoding is required for conversion, but nsstringencoding does not include GBK. What should I do? Use another method:
Nsstringencoding gbkencoding = encode (kcfstringencodinggb_18030_2000); nsstring * pagesource = [[nsstring alloc] initwithdata: pagedata encoding: gbkencoding];
Why? In nsstring. H (hold down the command and double-click nsstringencoding to view it), in the definition of nsstringencoding, the comment says:
Note that in addition to the values explicitly listed below, nsstringencoding supports encodings provided by cfstring.
See cfstringencodingext. h for a list of these encodings.
See cfstring. h for functions which convert between nsstringencoding and cfstringencoding.