通过 MFMailcomposeController 发送时在 sqlite 中发现空数据

Found empty data in sqlite when send via MFMailcomposeController

我正在通过电子邮件发送 SQLite(Coredata sqlite 文件) 文件,但在收到的邮件中发现所有表中的数据都是空的。

我正在使用以下代码:

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:subject];
[mc setMessageBody:body isHTML:FALSE];
if (attachmentData) {
 [mc addAttachmentData:attachmentData mimeType:fileMimeType fileName:fileName];
}
if (!recipients || recipients.count == 0) {
 recipients = @[];
}
[mc setToRecipients:recipients];
[presentedViewController presentViewController:mc animated:YES completion:nil];

Here, fileMimeType = "application/x-sqlite3" and fileName: xyz.sqlite

在这里找到相同的 question,但没有解决方案。知道怎么做吗?

  • 您确定您的 attachmentData 不为零吗?
  • 您的附件数据有多大?
  • 您如何从 sqlite 文件构建数据?

发送一个简单的示例数据库,这个测试代码对我来说似乎工作正常:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self sendSampleMailWithDbAttached];
}

- (void)sendSampleMailWithDbAttached {
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:@"Message Subject"];
    [mc setMessageBody:@"Message Body" isHTML:NO];
    NSString *sqliteFilePath = [self createTestDb];
    NSData *attachmentData = [NSData dataWithContentsOfFile:sqliteFilePath];
    if (attachmentData) {
        [mc addAttachmentData:attachmentData mimeType:@"application/x-sqlite3" fileName:@"xyz.sqlite"];
    }
    [mc setToRecipients:@[@"fake@email.com"]];
    [self presentViewController:mc animated:YES completion:nil];
}

- (NSString *)createTestDb {
    NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
                              stringByAppendingPathComponent: @"xyz.sqlite"];
    sqlite3 *db;
    if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) {
        NSString *query = @"CREATE TABLE IF NOT EXISTS test(abc TEXT, def TEXT);";
        if (sqlite3_exec(db, [query UTF8String], NULL, NULL, NULL) == SQLITE_OK) {
            query = @"INSERT INTO test(abc, def) VALUES('ABC', '123');";
        }
        sqlite3_close(db);
    }
    return databasePath;
}

正如我在评论中指出的那样,iOS 默认使用 SQLite 的 WAL 模式,因此您需要将 WAL 文件作为附件包含在内。