I can successfully add an attachment to an email with my iOS app, but only if I hard code the file name, otherwise there is no data in the attachment.
This works:
(IBAction)sendEmail:(id)sender
{
transectNameString = [[NSUserDefaults standardUserDefaults] objectForKey:@"dipstransect"];
transectFileExt = [NSString stringWithFormat:@".csv"];
NSString *string1 = transectNameString;
NSString *string2 = @" Drops";
NSString *string3 = transectFileExt;
NSString *tempstring1 = [string1 stringByAppendingString:string2];
setTransectName = [tempstring1 stringByAppendingString:string3];
MFMailComposeViewController *mailComposer;
NSArray *emailAddresses;
mailComposer = [MFMailComposeViewController new];
emailAddresses = @[];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:emailAddresses];
filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
fileName = @"Paynes Drops.csv";
fileAtPath = [filePath stringByAppendingPathComponent:fileName];
NSData *fileData = [NSData dataWithContentsOfFile:fileAtPath];
[mailComposer addAttachmentData:fileData mimeType:@"text/csv" fileName:fileName];
[self presentViewController:mailComposer animated:YES completion:nil];
}
This does not work:
(IBAction)sendEmail:(id)sender
{
transectNameString = [[NSUserDefaults standardUserDefaults] objectForKey:@"dipstransect"];
transectFileExt = [NSString stringWithFormat:@".csv"];
NSString *string1 = transectNameString;
NSString *string2 = @" Drops";
NSString *string3 = transectFileExt;
NSString *tempstring1 = [string1 stringByAppendingString:string2];
setTransectName = [tempstring1 stringByAppendingString:string3];
MFMailComposeViewController *mailComposer;
NSArray *emailAddresses;
mailComposer = [MFMailComposeViewController new];
emailAddresses = @[];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:emailAddresses];
filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
fileName = setTransectName;
fileAtPath = [filePath stringByAppendingPathComponent:fileName];
NSData *fileData = [NSData dataWithContentsOfFile:fileAtPath];
[mailComposer addAttachmentData:fileData mimeType:@"text/csv" fileName:fileName];
[self presentViewController:mailComposer animated:YES completion:nil];
}
What is it about the fileName that causes the problem?
I moved the following line to the ViewDidLoad statement and it worked.
(void)viewDidLoad {
transectNameString = [[NSUserDefaults standardUserDefaults] objectForKey:@"dipstransect"];
[super viewDidLoad]; // Do any additional setup after loading the view. }
(IBAction)sendEmail:(id)sender {
MFMailComposeViewController *mailComposer; NSArray *emailAddresses;
mailComposer = [MFMailComposeViewController new]; emailAddresses = @[]; mailComposer.mailComposeDelegate = self; [mailComposer setToRecipients:emailAddresses];
filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
transectFileExt = [NSString stringWithFormat:@".csv"]; NSString *string2 = @" Drops";
NSString *tempString1 = [transectNameString stringByAppendingString:string2]; NSString *tempString2 = [tempString1 stringByAppendingString:transectFileExt];
fileName = tempString2; fileAtPath = [filePath stringByAppendingPathComponent:fileName]; NSData *fileData = [NSData dataWithContentsOfFile:fileAtPath];
[mailComposer addAttachmentData:fileData mimeType:@"text/csv" fileName:fileName];
transectFile2Ext = [NSString stringWithFormat:@".txt"]; NSString *string4 = @" Drops Notes";
NSString *tempString3 = [transectNameString stringByAppendingString:string4]; NSString *tempString4 = [tempString3 stringByAppendingString:transectFile2Ext];
file2Name = tempString4; file2AtPath = [filePath stringByAppendingPathComponent:file2Name]; NSData *file2Data = [NSData dataWithContentsOfFile:file2AtPath];
[mailComposer addAttachmentData:file2Data mimeType:@"text/txt" fileName:file2Name];
[self presentViewController:mailComposer animated:YES completion:nil];
}