I'm trying to show content of text file with unknown encoding according to Apple's documentation:
Try stringWithContentsOfFile:usedEncoding:error: or initWithContentsOfFile:usedEncoding:error: (or the URL-based equivalents). These methods try to determine the encoding of the resource, and if successful return by reference the encoding used.
If (1) fails, try to read the resource by specifying UTF-8 as the encoding.
If (2) fails, try an appropriate legacy encoding. "Appropriate" here depends a bit on circumstances; it might be the default C string encoding, it might be ISO or Windows Latin 1, or something else, depending on where your data is coming from.
This is not always working. Is there more reliable ways to detect encoding?
You should use NSAttributedString which can detect encoding. After long time testing different solutions, I use that:
NSError *error;
NSDictionary *options = [NSDictionary dictionary];
NSDictionary *attributes;
NSAttributedString *theString = [[NSAttributedString alloc] initWithURL:fileURL options:options documentAttributes:&attributes error:&error];
NSInteger detectedEncoding = [[attributes objectForKey:@"CharacterEncoding"] integerValue];
I tested many files from many sources/environment, and it seem to be efficient (thus you should check whether error is nil or not). For a plain csv file exported from Excel, I get this attributes dictionary (30 value means NSMacOSRomanStringEncoding:
{
CharacterEncoding = 30;
DocumentType = NSPlainText;
UTI = "public.plain-text";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With