【问题标题】:Attaching sqlite to xcode mail将 sqlite 附加到 xcode 邮件
【发布时间】:2012-07-16 09:27:25
【问题描述】:

这段代码我快疯了,它应该可以工作,但是当我收到邮件时没有附加文件,这是我的代码:

-(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];
    }
}

路径没问题,数据库中有数据,我在撰写邮件时也看到了该文件,但没有任何东西到达我的邮件。我想问题就在这里

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

但不知道为什么它不起作用,谢谢帮助

【问题讨论】:

标签: iphone database xcode sqlite email


【解决方案1】:

我已经在Error sending database from xCode 更新了答案,但您似乎没有检查它,反正又来了:

-(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];

从您的代码中注释了这些:

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

并替换为以下内容

    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];
}   

}

【讨论】:

  • 有趣...- (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename 支持的 mime 类型在此处列出 iana.org/assignments/media-types/media-types.xhtml。没有application/x-sqlite3 类型。将在真实环境中尝试代码。
  • 这个可以附加.db文件吗?
  • 是的,它应该能够附加任何文件,只要您更改文件名、扩展名和 mimeType,在这种情况下应该是 application/octet-stream 并且该文件是可读的通过 iOS。请在尝试后提供反馈,以便其他用户在需要时找到答案。
  • 在 SQLite 中得到空数据
  • 您能否发布此解决方案的快速版本@XIII
猜你喜欢
  • 1970-01-01
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 2019-04-19
  • 2016-08-29
  • 2017-09-17
  • 2014-06-25
相关资源
最近更新 更多