iOS 获取网页html(NSStringEncoding)编码失败

通过网页地址获取html失败

NSURL *url = [[NSURL alloc] initWithString:str];
NSError *error;
NSString *xml = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];

错误提示:NSCocoaErrorDomain Code=261 "The file “item.htm” couldn’t be opened using text encoding Unicode (UTF-8)."
解决方式:

NSURL *url = [[NSURL alloc] initWithString:str];
NSError *error;
NSStringEncoding encoder;
NSString *xmlStr = [NSString stringWithContentsOfURL:url encoding:encoder=-2147482062 error:&error];

原因:编码格式不对
我是在获取淘宝天猫商品链接的html是出现这个问题,在解决这个问题的途中,尝试了下面所有的编码格式,不是抛错就是乱码,懵逼了

typedef NSUInteger NSStringEncoding;
NS_ENUM(NSStringEncoding) {
    NSASCIIStringEncoding = 1,      /* 0..127 only */
    NSNEXTSTEPStringEncoding = 2,
    NSJapaneseEUCStringEncoding = 3,
    NSUTF8StringEncoding = 4,
    NSISOLatin1StringEncoding = 5,
    NSSymbolStringEncoding = 6,
    NSNonLossyASCIIStringEncoding = 7,
    NSShiftJISStringEncoding = 8,          /* kCFStringEncodingDOSJapanese */
    NSISOLatin2StringEncoding = 9,
    NSUnicodeStringEncoding = 10,
    NSWindowsCP1251StringEncoding = 11,    /* Cyrillic; same as AdobeStandardCyrillic */
    NSWindowsCP1252StringEncoding = 12,    /* WinLatin1 */
    NSWindowsCP1253StringEncoding = 13,    /* Greek */
    NSWindowsCP1254StringEncoding = 14,    /* Turkish */
    NSWindowsCP1250StringEncoding = 15,    /* WinLatin2 */
    NSISO2022JPStringEncoding = 21,        /* ISO 2022 Japanese encoding for e-mail */
    NSMacOSRomanStringEncoding = 30,

    NSUTF16StringEncoding = NSUnicodeStringEncoding,      /* An alias for NSUnicodeStringEncoding */

    NSUTF16BigEndianStringEncoding = 0x90000100,          /* NSUTF16StringEncoding encoding with explicit endianness specified */
    NSUTF16LittleEndianStringEncoding = 0x94000100,       /* NSUTF16StringEncoding encoding with explicit endianness specified */

    NSUTF32StringEncoding = 0x8c000100,                   
    NSUTF32BigEndianStringEncoding = 0x98000100,          /* NSUTF32StringEncoding encoding with explicit endianness specified */
    NSUTF32LittleEndianStringEncoding = 0x9c000100        /* NSUTF32StringEncoding encoding with explicit endianness specified */
};

因为上面的编码格式只是常用的,并不是所有的。通过下面的代码获取到所有的编码方式

const NSStringEncoding *encodings = [NSString availableStringEncodings];
NSMutableString *str = [[NSMutableString alloc] init];
NSStringEncoding encoding;
while ((encoding = *encodings++) != 0)
{
[str appendFormat: @"%@ === %in", [NSString localizedNameOfStringEncoding:encoding], encoding]; }
[result setString: str];

所有的编码格式

Western (Mac OS Roman) === 30
Japanese (Mac OS) === -2147483647
Traditional Chinese (Mac OS) === -2147483646
Korean (Mac OS) === -2147483645
Arabic (Mac OS) === -2147483644
Hebrew (Mac OS) === -2147483643
Greek (Mac OS) === -2147483642
Cyrillic (Mac OS) === -2147483641
Devanagari (Mac OS) === -2147483639
Gurmukhi (Mac OS) === -2147483638
Gujarati (Mac OS) === -2147483637
Thai (Mac OS) === -2147483627
Simplified Chinese (Mac OS) === -2147483623
Tibetan (Mac OS) === -2147483622
Central European (Mac OS) === -2147483619
Symbol (Mac OS) === 6
Dingbats (Mac OS) === -2147483614
Turkish (Mac OS) === -2147483613
Croatian (Mac OS) === -2147483612
Icelandic (Mac OS) === -2147483611
Romanian (Mac OS) === -2147483610
Celtic (Mac OS) === -2147483609
Gaelic (Mac OS) === -2147483608
Keyboard Symbols (Mac OS) === -2147483607
Farsi (Mac OS) === -2147483508
Cyrillic (Mac OS Ukrainian) === -2147483496
Inuit (Mac OS) === -2147483412
Unicode (UTF-32LE) === -1677721344
Unicode (UTF-8) === 4
Unicode (UTF-16) === 10
Unicode (UTF-16BE) === -1879047936
Unicode (UTF-16LE) === -1811939072
Unicode (UTF-32) === -1946156800
Unicode (UTF-32BE) === -1744830208
Western (ISO Latin 1) === 5
Central European (ISO Latin 2) === 9
Western (ISO Latin 3) === -2147483133
Central European (ISO Latin 4) === -2147483132
Cyrillic (ISO 8859-5) === -2147483131
Arabic (ISO 8859-6) === -2147483130
Greek (ISO 8859-7) === -2147483129
Hebrew (ISO 8859-8) === -2147483128
Turkish (ISO Latin 5) === -2147483127
Nordic (ISO Latin 6) === -2147483126
Thai (ISO 8859-11) === -2147483125
Baltic Rim (ISO Latin 7) === -2147483123
Celtic (ISO Latin) === -2147483122
Western (ISO Latin 9) === -2147483121
Romanian (ISO Latin 10) === -2147483120
Latin-US (DOS) === -2147482624
Greek (DOS) === -2147482619
Baltic Rim (DOS) === -2147482618
Western (DOS Latin 1) === -2147482608
Greek (DOS Greek 1) === -2147482607
Central European (DOS Latin 2) === -2147482606
Cyrillic (DOS) === -2147482605
Turkish (DOS) === -2147482604
Portuguese (DOS) === -2147482603
Icelandic (DOS) === -2147482602
Hebrew (DOS) === -2147482601
Canadian French (DOS) === -2147482600
Arabic (DOS) === -2147482599
Nordic (DOS) === -2147482598
Cyrillic (DOS) === -2147482597
Greek (DOS Greek 2) === -2147482596
Thai (Windows, DOS) === -2147482595
Japanese (Windows, DOS) === 8
Simplified Chinese (Windows, DOS) === -2147482591
Korean (Windows, DOS) === -2147482590
Traditional Chinese (Windows, DOS) === -2147482589
Western (Windows Latin 1) === 12
Central European (Windows Latin 2) === 15
Cyrillic (Windows) === 11
Greek (Windows) === 13
Turkish (Windows Latin 5) === 14
Hebrew (Windows) === -2147482363
Arabic (Windows) === -2147482362
Baltic Rim (Windows) === -2147482361
Vietnamese (Windows) === -2147482360
Western (ASCII) === 1
Japanese (Shift JIS X0213) === -2147482072
Chinese (GBK) === -2147482063
Chinese (GB 18030) === -2147482062
Japanese (ISO 2022-JP) === 21
Korean (ISO 2022-KR) === -2147481536
Japanese (EUC) === 3
Simplified Chinese (EUC) === -2147481296
Traditional Chinese (EUC) === -2147481295
Korean (EUC) === -2147481280
Japanese (Shift JIS) === -2147481087
Cyrillic (KOI8-R) === -2147481086
Traditional Chinese (Big 5) === -2147481085
Western (Mac Mail) === -2147481084
Simplified Chinese (HZ GB 2312) === -2147481083
Traditional Chinese (Big 5 HKSCS) === -2147481082
Ukrainian (KOI8-U) === -2147481080
Traditional Chinese (Big 5-E) === -2147481079
Western (NextStep) === 2
Non-lossy ASCII === 7
Western (EBCDIC Latin 1) === -2147480574

多看看,多试试,找到对应的编码格式就解决问题
其他的NSStringEncoding编码问题也应该可以参考
参考:http://www.cnblogs.com/zhwl/archive/2012/12/31/2840746.html

你可能感兴趣的:(iOS 获取网页html(NSStringEncoding)编码失败)