问题
I am sending SQLite(Coredata sqlite file) file via email but found empty data in all tables in the received mail.
I am using below code:
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
Found the same question here, but with no solution. Any idea how to do that?
回答1:
As I indicated in comments, iOS uses SQLite’s WAL mode by default, so you need to include the WAL file as an attachment.
回答2:
- Are you sure that you attachmentData isn't nil?
- How large is your attachmentData?
- How are you constructing your data from the sqlite file?
This test code seemed to work fine for me with sending a simple sample database:
- (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;
}
来源:https://stackoverflow.com/questions/56151110/found-empty-data-in-sqlite-when-send-via-mfmailcomposecontroller