当用户点击exportAllData按钮时,我试图通过位于文档目录中的电子邮件导出SQLite文件。使用下面的代码,我能够打开邮件应用程序并附加文件。但是,当我发送它时,该文件不会随电子邮件一起发送。
- (IBAction)exportAllDataButtonPressed:(id)sender
{
MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
composer.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail])
{
[composer setToRecipients:[NSArray arrayWithObjects:@"[email protected]",nil]];
[composer setSubject:@"SQLite File"];
[composer setMessageBody:@"This is your SQLite file" isHTML:NO];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *path = [documentDirectory stringByAppendingPathComponent:@"mooddb.sql"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[composer addAttachmentData:myData mimeType:@"application/x-sqlite3" fileName:path];
[self presentViewController:composer animated:YES completion:nil];
}
}
[composer addAttachmentData:myData mimeType:@"application/x-sqlite3" fileName:path];
在FileName中你正在发送路径...... 所以plz chege路径到文件名.. 喜欢..
[composer addAttachmentData:myData mimeType:@"application/x-sqlite3" fileName:@"mooddb.sql"];
事实证明我在错误的目录中搜索。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
应该:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
Swift版本
@IBAction func exportAllDataButtonTapped(_ sender: Any) {
guard MFMailComposeViewController.canSendMail() == true else { return }
let mailCompose = MFMailComposeViewController()
mailCompose.mailComposeDelegate = self
mailCompose.setSubject("Sqlite File")
mailCompose.setMessageBody("", isHTML: false)
if let documentsPathURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileName = "myDatabase.sqlite"
let fullURL = documentsPathURL.appendingPathComponent(fileName)
if let data = try? Data(contentsOf: fullURL) {
mailCompose.addAttachmentData(data, mimeType: "application/x-sqlite3", fileName: fileName)
}
}
present(mailCompose, animated: true, completion: nil)
}