2017-05-09 118 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!") 
}