I'm loading a website HTML using this call -
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"utf-8" forHTTPHeaderField:@"Accept-Encoding"];
[request setValue:@"text/html" forHTTPHeaderField:@"Accept"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { ... }
and then, to convert NSData into NSString, I need to know the encoding, so I call -
NSString *textEncoding = [response textEncodingName];
from the code block, but it returns nil on websites that won't specify "Content-Encoding" header field.
If I don't know the encoding, [[NSString alloc] initWithData:data encoding:responseEncoding]
won't give me readable HTML.
How can I detect the right encoding for websites that don't send "Content-Encoding" header field?