如何从UI Label(Swift 2 IOS 9)发送应用程序中的电子邮件?

时间:2016-01-02 21:10:18

标签: ios swift email

操纵本教程http://www.raywenderlich.com/113772/uisearchcontroller-tutorial

我有一个显示人物的表格视图,当单击该单元格时,用户被重定向到另一个显示其图片和电子邮件的视图。 我希望用户能够点击电子邮件地址并通过电子邮件发送。我研究过并找到了类似的教程 https://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift/

上面的教程的问题是当测试运行新代码时ios模拟器弹出错误并且不会显示组合的电子邮件(也许是故障?)并且如果模拟器没有给出错误我不知道如何显示基于用户选择的人的多封电子邮件。任何解决这个问题或任何替代方案的帮助都会很棒谢谢!!

1 个答案:

答案 0 :(得分:1)

当您尝试打开邮件时,模拟器将崩溃。请在实际设备上试用。

撰写邮件时请执行以下操作 将这些添加到您的班级 MFMessageComposeViewControllerDelegateMFMailComposeViewControllerDelegate

didSelectRowAtIndexPath中,这是在tableView中按行时调用的函数。执行以下操作:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    let candy = candies[indexPath.row]
    var mail: MFMailComposeViewController!

    // yourArray is the array that you use to populate the tableView
    // .mail is the variable in the object (I´m assuming you´re using objects in your array)
    let toRecipients = [candy[indexPath.row].email]
    let subject = "Feedback"
    let body = "<br><br><p>I have a \(UIDevice.currentDevice().modelName).<br> And iOS version \(UIDevice.currentDevice().systemVersion).<br</p>"

    mail = MFMailComposeViewController()
    mail.mailComposeDelegate = self
    mail.setToRecipients(toRecipients)
    mail.setSubject(subject)
    mail.setMessageBody(body, isHTML: true)

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

如果您需要使用它们,请使用下面的委托方法

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

    func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
        switch (result.rawValue) {
        case MessageComposeResultCancelled.rawValue:
            self.dismissViewControllerAnimated(true, completion: nil)
        case MessageComposeResultFailed.rawValue:
            self.dismissViewControllerAnimated(true, completion: nil)
        case MessageComposeResultSent.rawValue:
            self.dismissViewControllerAnimated(true, completion: nil)
        default:
            break;
        }
    }