4

I am going crazy with this code, It should be working but when I receive the mail there is no file attached, here is my code:

-(IBAction)mandar:(id)sender
{   
    MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
    [composer setMailComposeDelegate:self];
    if ([MFMailComposeViewController canSendMail]) 
    {
        [composer setToRecipients:[NSArray arrayWithObjects:@"tuperroensalsa@hotmail.com",nil]];
        [composer setSubject:@"Base de datos"];
        [composer setMessageBody:@"Mensage" isHTML:NO];
        [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
            NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentPath = [searchPaths objectAtIndex:0];
        NSString *path = [[NSString alloc] initWithFormat:@"%@/capturas.sqlite",documentPath];

        NSString *Newpath = [[NSString alloc] initWithFormat:@"%@/newData.sqlite",documentPath];
        [[NSFileManager defaultManager] copyItemAtPath:path toPath:Newpath error:nil];
        NSData *data = [NSData dataWithContentsOfFile:Newpath];
        [composer addAttachmentData:data mimeType:@"application/x-sqlite3" fileName:@"capturas.sqlite"];

        [self presentModalViewController:composer animated:YES];
    }
    else {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error" message:@"No se a podido mandar el mensage" delegate:self cancelButtonTitle:@"dismis" otherButtonTitles:nil, nil];
        [alert show];
    }
}

The path is ok and the database has data in it, I also see the file when I am composing the mail but nothing arrives to my mail. I guess the problem is here

[composer addAttachmentData:data mimeType:@"application/x-sqlite3" fileName:@"capturas.sqlite"];

but dont know why it doesnt works, thx for the help

2

1 Answer 1

9

I've updated the answer already at: Error sending database from xCode but it seems you didn't check it, anyway here it's again:

-(IBAction)mandar:(id)sender {

MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) 
{
    [composer setToRecipients:[NSArray arrayWithObjects:@"EMAIL Address here",nil]];
    [composer setSubject:@"Base de datos"];
    [composer setMessageBody:@"Mensage" isHTML:YES];
    [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

Commented these from your code:

//  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//  NSString *documentsDirectory = [paths objectAtIndex:0];
//  NSString *file = [documentsDirectory stringByAppendingPathComponent:@"capturas.sqlite"];
//  NSData *data=[NSData dataWithContentsOfFile:file];

And replaced with the following

    NSString *path = [[NSBundle mainBundle] pathForResource:@"database name without extension" ofType:@"sqlite"];
    NSData *myData = [NSData dataWithContentsOfFile:path];

    [composer addAttachmentData:myData mimeType:@"application/x-sqlite3" fileName:path];

    [self presentModalViewController:composer animated:YES];
}
else {
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error" message:@"No se a podido mandar el mensage" delegate:self cancelButtonTitle:@"dismis" otherButtonTitles:nil, nil];
    [alert show];
}   

}

9
  • interesting... the mime types supported by - (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename are listed here iana.org/assignments/media-types/media-types.xhtml . There is no application/x-sqlite3 type. Will try the code on a real environment.
    – kas-kad
    Aug 18, 2014 at 21:17
  • is this able to attach .db file ? Oct 9, 2014 at 23:40
  • Yes, it should be able to attach any file as long as you will change the file name, extension and mimeType which should be application/octet-stream in that case and as long as that file is readable by iOS. Please give the feedback after trying this so other users can find the answer if they needed it.
    – XIII
    Oct 11, 2014 at 11:45
  • Got empty data in SQLite May 15, 2019 at 13:37
  • Can you please post swift version of this solution @XIII
    – Codicil
    Jan 31, 2020 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.