· 6 years ago · Jun 20, 2019, 06:20 PM
1MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
2mc.mailComposeDelegate = self;
3[mc setSubject:subject];
4[mc setMessageBody:body isHTML:FALSE];
5if (attachmentData) {
6 [mc addAttachmentData:attachmentData mimeType:fileMimeType fileName:fileName];
7}
8if (!recipients || recipients.count == 0) {
9 recipients = @[];
10}
11[mc setToRecipients:recipients];
12[presentedViewController presentViewController:mc animated:YES completion:nil];
13
14- (void)viewDidAppear:(BOOL)animated {
15 [super viewDidAppear:animated];
16 [self sendSampleMailWithDbAttached];
17}
18
19- (void)sendSampleMailWithDbAttached {
20 MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
21 mc.mailComposeDelegate = self;
22 [mc setSubject:@"Message Subject"];
23 [mc setMessageBody:@"Message Body" isHTML:NO];
24 NSString *sqliteFilePath = [self createTestDb];
25 NSData *attachmentData = [NSData dataWithContentsOfFile:sqliteFilePath];
26 if (attachmentData) {
27 [mc addAttachmentData:attachmentData mimeType:@"application/x-sqlite3" fileName:@"xyz.sqlite"];
28 }
29 [mc setToRecipients:@[@"fake@email.com"]];
30 [self presentViewController:mc animated:YES completion:nil];
31}
32
33- (NSString *)createTestDb {
34 NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
35 stringByAppendingPathComponent: @"xyz.sqlite"];
36 sqlite3 *db;
37 if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) {
38 NSString *query = @"CREATE TABLE IF NOT EXISTS test(abc TEXT, def TEXT);";
39 if (sqlite3_exec(db, [query UTF8String], NULL, NULL, NULL) == SQLITE_OK) {
40 query = @"INSERT INTO test(abc, def) VALUES('ABC', '123');";
41 }
42 sqlite3_close(db);
43 }
44 return databasePath;
45}