Problem Statement
I create a number of strings, concatenate them together into CSV format, and then email the string as an attachment.
When these strings contain only ASCII characters, the CSV file is built and emailed properly. When I include non-ASCII characters, the result string becomes malformed and the CSV file is not created properly. (The email view shows an attachment, but it is not sent.)
For instance, this works:
uncle bill's house of pancakes
But this doesn't (note the curly apostrophe):
uncle bill’s house of pancakes
Question
How do I create and encode the final string properly so that all valid unicode characters are included and the result string is formed properly?
Notes
The strings are created via a UITextField and then are written to and then read from a Core Data store.
This suggests that the problem lies in the initial creation and encoding of the string: NSString unicode encoding problem
I don't want to have to do this: remove non ASCII characters from NSString in objective-c
The strings are written and read to/from the data store fine. The strings display properly (individually) in the app's table views. The problem only manifests when concatenating the strings together for the email attachment.
String Processing Code
I concatenate my strings together like this:
[reportString appendFormat:@"%@,", category];
[reportString appendFormat:@"%@,", client];
[reportString appendFormat:@"%@\n", detail];
etc.
Replacing curly quotes with boring quotes makes it work, but I don't want to do it this way:
- (NSMutableString *)cleanString:(NSString *)activity {
NSString *temp1 = [activity stringByReplacingOccurrencesOfString:@"’" withString:@"'"];
NSString *temp2 = [temp1 stringByReplacingOccurrencesOfString:@"‘" withString:@"'"];
NSString *temp3 = [temp2 stringByReplacingOccurrencesOfString:@"”" withString:@"\""];
NSString *temp4 = [temp3 stringByReplacingOccurrencesOfString:@"“" withString:@"\""];
return [NSMutableString temp4];
}
Edit: The email is sent:
NSString *attachment = [self formatReportCSV];
[picker addAttachmentData:[attachment dataUsingEncoding:NSStringEncodingConversionAllowLossy] mimeType:nil fileName:@"MyCSVFile.csv"];
where formatReportCSV
is the function that concatenates and returns the csv string.