原文摘自:http://www.cnblogs.com/kiao295338444/articles/2590031.html
我們知道,使用NSURLConnection的代理方法下載網頁,存到一個NSData中,
NSMutableData *pageData;
[pageData appendData:data];
如果網頁編碼是UTF-8的,可以這麼轉換為字串:
NSString *pageSource = [[NSString alloc] initWithData:pageData encoding:NSUTF8StringEncoding];
如果網頁是gbk(或者gb2312),用UTF8轉換的話,pageSource返回nil。這時需要使用gbk編碼做轉換,但是NSStringEncoding不含gbk,怎麼辦?用另一個方法處理一下:
NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); NSString*pageSource = [[NSString alloc] initWithData:pageData encoding:gbkEncoding];
為什麼可以這麼處理?在NSString.h(按住command,雙擊NSStringEncoding即能查看),對NSStringEncoding的定義中,注釋這麼寫著:
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.