Search code examples
iossqliteexportmfmailcomposeviewcontroller

Export SQLite file in document directory via email


I am trying to export a SQLite file via email located in the document directory when a user taps on the exportAllData button. Using the code below, I was able to open up the mail app and attach a file. However when I send it, the file does not get sent with the email.

- (IBAction)exportAllDataButtonPressed:(id)sender
{
    MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
    composer.mailComposeDelegate = self;
    if ([MFMailComposeViewController canSendMail])
    {
    [composer setToRecipients:[NSArray arrayWithObjects:@"[email protected]",nil]];
    [composer setSubject:@"SQLite File"];
    [composer setMessageBody:@"This is your SQLite file" isHTML:NO];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

    NSString *documentDirectory = [paths objectAtIndex:0];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"mooddb.sql"];
    NSData *myData = [NSData dataWithContentsOfFile:path];

    [composer addAttachmentData:myData mimeType:@"application/x-sqlite3" fileName:path];
    [self presentViewController:composer animated:YES completion:nil];
    }
}

Solution

  • Turns out I was searching in the wrong directory.

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    

    should be:

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);