Razgriz
Razgriz

Reputation: 7343

iOS 7 - generated CSV file not attaching to email properly

Sorry for the confusing title. I have an application that has a database. I think I am able to export a database table as a csv file. However, I'm having difficulties attaching the sent file. Whenever I check my yahoo mail, the paper clip shows, telling me that there's an attached file. However, when I open the message, there are no files in the message.

This is my function that creates the csv file:

- (IBAction)saveToCSV:(id)sender {

    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsPath = [paths objectAtIndex:0];
    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"itemList.db"];

    FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
    [database open];

    FMResultSet *results = [database executeQuery:@"SELECT * FROM OrderList"];

    //CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initWithCSVFile:@"/csvFile/" atomic:NO];
    CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initForWritingToCSVFile:@"/Orderlist.csv"];

    while([results next]) {
        NSDictionary *resultRow = [results resultDictionary];
        NSArray *orderedKeys = [[resultRow allKeys] sortedArrayUsingSelector:@selector(compare:)];
        //iterate over the dictionary
        for (NSString *columnName in orderedKeys) {
            id value = [resultRow objectForKey:columnName];
            [csvWriter writeField:value];
        }
        [csvWriter finishLine];
    }
    [csvWriter closeStream];
    //[csvWriter release];

    [database close];

    NSLog(@"csv completed");
    [self sendEmail];

}

and here is my sendEmail function

- (void) sendEmail{
    if ([MFMailComposeViewController canSendMail]) {
        NSArray *sendTo = [NSArray arrayWithObject:@"My Name <[email protected]>"];
        MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
        mailViewController.mailComposeDelegate = self;
        [mailViewController setSubject:@"Order CSV."];
        [mailViewController setMessageBody:@"Your message goes here." isHTML:NO];
        [mailViewController setToRecipients:sendTo];
        [mailViewController addAttachmentData:[NSData dataWithContentsOfFile:@"/Orderlist.csv"]
                                     mimeType:@"text/csv"
                                     fileName:@"Orderlist.csv"];


        [self presentModalViewController:mailViewController animated:YES];

    }
    else {
        NSLog(@"Device is unable to send email in its current state.");
        UIAlertView *message = [[UIAlertView alloc]
                                initWithTitle:@"Failed to Send Mail!"
                                message:@"Please make sure that you have logged in to the Mail."
                                delegate:nil
                                cancelButtonTitle:@"OK"
                                otherButtonTitles:nil];

        [message show];

    }
}

I tried changing the file path and file name, but to no avail.

Any ideas anyone?

Upvotes: 1

Views: 495

Answers (1)

Tim Sawtell
Tim Sawtell

Reputation: 307

Looks like you're writing the CSV file (or think you are) to a path that you don't have access to, i.e. root.

iOS apps are sandboxed and you may only write to certain directories, so in your case you should be writing the csv file to somewhere in the caches directory.

Change

CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initForWritingToCSVFile:@"/Orderlist.csv"];

to

NSString *csvPath = [docsPath stringByAppendingPathComponent:@"Orderlist.csv"];
CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initForWritingToCSVFile:csvPath];

And change

[mailViewController addAttachmentData:[NSData dataWithContentsOfFile:@"/Orderlist.csv"]
                                 mimeType:@"text/csv"
                                 fileName:@"Orderlist.csv"];

to

NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *csvPath = [docsPath stringByAppendingPathComponent:@"Orderlist.csv"];
[mailViewController addAttachmentData:[NSData dataWithContentsOfFile:csvPath]
                                 mimeType:@"text/csv"
                                 fileName:@"Orderlist.csv"];

Upvotes: 3

Related Questions