2017-05-09 100 views
0

我的问题是,我编程的电子邮件窗口,它确实发送电子邮件,但是当他们发送或我想关闭窗口没有发生什么事情。电子邮件已完成但发送电子邮件窗口未关闭?

那是我的代码:

import Foundation 
import UIKit 
import MessageUI 


class ContactViewController: UIViewController, MFMailComposeViewControllerDelegate, UIAlertViewDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

let mail = MFMailComposeViewController() 


    @IBAction func email(_ sender: Any) { 

     if !MFMailComposeViewController.canSendMail() { 
      let warnung = UIAlertController(title: "Email konnte nicht gesendet werden", message: "Dein Gerät unterstützt leider keine Email-Funktion.", preferredStyle: .alert) 
      let action1 = UIAlertAction(title: "OK", style: .default, handler: nil) 
      warnung.addAction(action1) 
      self.present(warnung, animated: true, completion: nil) 
      return 

      } else { 

       mail.mailComposeDelegate = self 
       mail.setToRecipients(["[email protected]"]) 
       mail.setSubject("Message to you") 
       mail.setMessageBody("Hello,\n", isHTML: false) 

       present(mail, animated: true, completion: nil) 

      func mailComposeController(_ controller: MFMailComposeViewController, 
             didFinishWithResult result: MFMailComposeResult, error: NSError?) { 
       mail.dismiss(animated: true, completion: nil) 
       print("Yes!") 
       } 


      } 
     } 
} 
邮件窗口的

这里'山截图: Just click on this link!

+0

为什么里面的'电子邮件的委托方法(_sender:)'方法,在它的'else'测试? – Larme

+0

因为我认为它在else方法中效果更好 – me21

+0

请注意,您的方法'didFinishWithResult'是在您的IBAction电子邮件方法中声明的。您需要将该方法移出该IBAction方法。它需要成为视图控制器的实例方法 –

回答

0
@IBAction func email(_ sender: Any) { 

    if !MFMailComposeViewController.canSendMail() { 
     let warnung = UIAlertController(title: "Email konnte nicht gesendet werden", message: "Dein Gerät unterstützt leider keine Email-Funktion.", preferredStyle: .alert) 
     let action1 = UIAlertAction(title: "OK", style: .default, handler: nil) 
     warnung.addAction(action1) 
     self.present(warnung, animated: true, completion: nil) 
     return 

     } else { 

      mail.mailComposeDelegate = self 
      mail.setToRecipients(["[email protected]"]) 
      mail.setSubject("Message to you") 
      mail.setMessageBody("Hello,\n", isHTML: false) 

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

func mailComposeController(_ controller: MFMailComposeViewController, 
            didFinishWithResult result: MFMailComposeResult, error: NSError?) { 
      mail.dismiss(animated: true, completion: nil) 
      print("Yes!") 
}