2017-05-09 2 views
0

私の問題は、emailwindowをプログラムして電子メールを送信することですが、送信した場合や、ウィンドウを閉じたい場合は何も起こりません。私のコードザッツメールは終了しましたが、メールの送信ウィンドウは閉じませんか?

:メールウィンドウの

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


      } 
     } 
} 

Here'saスクリーンショット:それでだ、 `メソッド: Just click on this link!

+0

なぜデリゲートメソッドは'電子メール(_sender)の内部にあります'else'テスト? – Larme

+0

elseメソッドの中にあるとうまくいくと思ったから – me21

+0

あなたのメソッドdidFinishWithResultがあなたのIBAction電子メールメソッドの中で宣言されていることに注意してください。そのメソッドをそのIBActionメソッドから移動する必要があります。あなたのView Controllerのインスタンスメソッドである必要があります –

答えて

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!") 
} 
関連する問題