Ashwini Chougale Ashwini Chougale - 10 months ago 61
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];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};

NSAttributedString *decodedString;
decodedString = [[NSAttributedString alloc] initWithData:stringData

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 Source

You can add NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding) options:

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

Hope it will work.