10

に.txtの取り付けIは、ドキュメントフォルダに保存されている.txtファイルを持っていると私は-sendEmailメソッドの本体に次のコードでMFMailComposeViewControllerことによってそれを送信する:メール作曲が表示されたらMFMailComposeViewController

NSData *txtData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dataBase" ofType:@"txt"]]; 
     [mail addAttachmentData:txtData mimeType:@"text/plain" fileName:[NSString stringWithFormat:@"dataBase.txt"]]; 

は、私が見ることができますメール本体に添付しますが添付なしでこのメールを受け取ります。おそらく、.txt添付ファイルのMIMEタイプが間違っているか、このコードで何か問題がありますか?

おかげ

答えて

25
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];   
     NSString *txtFilePath = [documentsDirectory stringByAppendingPathComponent:@"abc.txt"]; 
NSData *noteData = [NSData dataWithContentsOfFile:txtFilePath]; 
     MFMailComposeViewController *_mailController = [[MFMailComposeViewController alloc] init]; 
     [_mailController setSubject:[NSString stringWithFormat:@"ABC"]]; 
     [_mailController setMessageBody:_messageBody 
           isHTML:NO]; 
     [_mailController setMailComposeDelegate:self]; 
     [_mailController addAttachmentData:noteData mimeType:@"text/plain" fileName:@"abc.txt"]; 

はそれがお役に立てば幸いです。

+0

ありがとう、同僚! – Alex

+0

@Alexey私の喜び:-) –

3

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 
    controller.dismiss(animated: true, completion: nil) 
} 

すると、このデリゲート

MFMailComposeViewControllerDelegate 
に加入するようにしてくださいスウィフト3では、あなたはこの

@IBAction func emailLogs(_ sender: Any) { 
    let allPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) 
    let documentsDirectory = allPaths.first! 
    let pathForLog = documentsDirectory.appending("/application.log") 

    if MFMailComposeViewController.canSendMail() { 
     let mail = MFMailComposeViewController() 
     mail.mailComposeDelegate = self; 
     mail.setToRecipients(["[email protected]"]) 
     mail.setSubject("Application Logs") 
     mail.setMessageBody("Please see attached", isHTML: true) 

     if let fileData = NSData(contentsOfFile: pathForLog) { 
      mail.addAttachmentData(fileData as Data, mimeType: "text/txt", fileName: "application.log") 
     } 

     self.present(mail, animated: true, completion: nil) 
    } 
} 

のような添付ファイル付きのメールを送信することができます。そして結果に作曲コントローラを却下します

関連する問題