【问题标题】:Export SQLite file in document directory via email通过电子邮件在文档目录中导出 SQLite 文件
【发布时间】:2015-02-25 05:29:37
【问题描述】:

当用户点击 exportAllData 按钮时,我正在尝试通过位于文档目录中的电子邮件导出 SQLite 文件。使用下面的代码,我能够打开邮件应用程序并附加一个文件。但是,当我发送它时,文件不会随电子邮件一起发送。

- (IBAction)exportAllDataButtonPressed:(id)sender
{
    MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
    composer.mailComposeDelegate = self;
    if ([MFMailComposeViewController canSendMail])
    {
    [composer setToRecipients:[NSArray arrayWithObjects:@"test@test.com",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];
    }
}

【问题讨论】:

标签: ios sqlite export mfmailcomposeviewcontroller


【解决方案1】:
[composer addAttachmentData:myData mimeType:@"application/x-sqlite3" fileName:path]; 

在 FileName 中,您正在发送路径...
所以请检查文件名的路径..
喜欢..

[composer addAttachmentData:myData mimeType:@"application/x-sqlite3" fileName:@"mooddb.sql"];  

【讨论】:

  • 这只是改变文件名。
【解决方案2】:

原来我在错误的目录中搜索。

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

应该是:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

【讨论】:

    【解决方案3】:

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 2012-06-27
      相关资源
      最近更新 更多