如何从我的iOS应用程序发送邮件 - SWIFT

如何从我的iOS应用程序发送邮件 -  SWIFT

问题描述:

我想从我的应用程序发送邮件。我正在使用SWIFT完成我的第一步,我已经陷入困境。我想按一个按钮打开邮件。你能告诉我怎么做按钮连接吗?我认为它应该是一个动作,但我不知道将它放在代码上

I want to send a mail from my application. I am doing my first steps with SWIFT and i have stuck at a point. I want to press a button and open up the mail. Can you please tell me how to do the button connection? I think it should be an action but I don't know where to put it on the code

import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

    func sendEmail() {
        let mailVC = MFMailComposeViewController()
        mailVC.mailComposeDelegate = self
        mailVC.setToRecipients([])
        mailVC.setSubject("Subject for email")
        mailVC.setMessageBody("Email message string", isHTML: false)

        presentViewController(mailVC, animated: true, completion: nil)
    }

    // MARK: - Email Delegate 

    func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
}


更改 sendEmail ,如下所示:

@IBAction func sendEmail(sender: AnyObject) {
    let mailVC = MFMailComposeViewController()
    mailVC.mailComposeDelegate = self
    mailVC.setToRecipients([])
    mailVC.setSubject("Subject for email")
    mailVC.setMessageBody("Email message string", isHTML: false)

    presentViewController(mailVC, animated: true, completion: nil)
}

并在接口构建器中连接这个动作的按钮

and connect in Interface builder your button to this action