rattletrap99
rattletrap99

Reputation: 1479

Attached file to email--file not there upon receipt

I want to attach a .csv file to an email so the user can send it. At this point, I know the file is actually created when I'm working in the simulator (I can find and open it), and it appears to be attached to the email which is created. However, since I can't send email from the sim, that's as far as I get there.

Deployed on my iPad, the indications are that the file is created and attached to the email (correctly titled file icon included in body of email). I can email it, but no file is attached when the email is received.

Here's the code I'm using to create and write to the file:

-(NSString *)dataFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"dataFilePath created");
    return [documentsDirectory stringByAppendingPathComponent:@"WMDGstats.csv"];
    NSLog(@"File path: %@", documentsDirectory);
}

-(void) createCSVFile
{
    NSLog(@"Top of createCSVFile");

//    [[NSFileManager defaultManager] removeItemAtPath:pngFilePath error:&error];


    if (![[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]])
    {
        [[NSFileManager defaultManager] createFileAtPath: [self dataFilePath] contents:nil attributes:nil];
        NSLog(@"File created");
    }

...yada, yada data creation



    NSFileHandle *handle;
    handle = [NSFileHandle fileHandleForWritingAtPath: [self dataFilePath] ];

    [handle truncateFileAtOffset:0];

    [handle writeData:[fullString dataUsingEncoding:NSUTF8StringEncoding]];

    [self mailCSV];
}

And here's the way I attach it to the email:

-(void) mailCSV
{
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
        mail.mailComposeDelegate = self;
        [mail setSubject:@"Subject"];
        [mail setMessageBody:@"message body" isHTML:NO];
        [mail setToRecipients:@[@"[email protected]"]];

        NSData *WMDGData = [NSData dataWithContentsOfFile:@"WMDGstats.csv"];
        [mail addAttachmentData:WMDGData mimeType:@"text.csv" fileName:@"WMDGstats.csv"];

        [self presentViewController:mail animated:YES completion:NULL];
    }
    else
    {
        NSLog(@"This device cannot send email");
    }
}

Any ideas?

Thanks for looking!

Edit:

Per rmaddy's kind guidance in his comment below, I supplied the full file path for the file:

    NSData *WMDGData = [NSData dataWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:@"WMDGstats.csv"]];

And it works perfectly--file created, attached, mailed and received!

Thanks rmaddy!!!

Upvotes: 0

Views: 38

Answers (1)

rmaddy
rmaddy

Reputation: 318874

The following line:

NSData *WMDGData = [NSData dataWithContentsOfFile:@"WMDGstats.csv"];

is probably returning nil because you need to specify the proper path to the file. Since you are saving the file in the Documents folder, you need to specify that full path.

Upvotes: 1

Related Questions