Ashwini Chougale Ashwini Chougale - 2 months ago 11
iOS Question

How to convert Chinese Html String into NSString

For converting html string into NSString i am using below function

Code Snippet :

+(NSString *)getStringFromHtmlString:(NSString *)str
{

NSData *stringData = [str dataUsingEncoding:NSUTF8StringEncoding];
//NSHTMLTextDocumentType
NSDictionary *options = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};

NSAttributedString *decodedString;
decodedString = [[NSAttributedString alloc] initWithData:stringData
options:options
documentAttributes:NULL
error:NULL];


return decodedString.string;
}


Above method encode English text in proper format

But I want to encode Chinese text.

When Chinese string encode using above method it returns

I want proper encoding of chinese text.

enter image description here

Answer

You can add NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding) options:

NSDictionary *options = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)};

Hope it will work.