ios - 使用 Swift 实现应用内电子邮件失败

标签 ios swift email-attachments mfmailcomposeviewcontroller

我想用swift实现应用内邮件。当我单击该按钮时,会弹出电子邮件窗口。但是,我无法发送电子邮件。而且,点击取消-删除草稿后,我无法返回到原来的屏幕。

import UIkit
import MessageUI


class Information : UIViewController, MFMailComposeViewControllerDelegate{

    var myMail: MFMailComposeViewController!

    @IBAction func sendReport(sender : AnyObject) {

        if(MFMailComposeViewController.canSendMail()){
            myMail = MFMailComposeViewController()

            //myMail.mailComposeDelegate

            // set the subject
            myMail.setSubject("My report")

            //To recipients
            var toRecipients = ["lipeilin@gatech.edu"]
            myMail.setToRecipients(toRecipients)

            //CC recipients
            var ccRecipients = ["tzhang85@gatech.edu"]
            myMail.setCcRecipients(ccRecipients)

            //CC recipients
            var bccRecipients = ["tzhang85@gatech.edu"]
            myMail.setBccRecipients(ccRecipients)

            //Add some text to the message body
            var sentfrom = "Email sent from my app"
            myMail.setMessageBody(sentfrom, isHTML: true)

            //Include an attachment
            var image = UIImage(named: "Gimme.png")
            var imageData = UIImageJPEGRepresentation(image, 1.0)

            myMail.addAttachmentData(imageData, mimeType: "image/jped", fileName:     "image")

            //Display the view controller
            self.presentViewController(myMail, animated: true, completion: nil)
        }
        else{
            var alert = UIAlertController(title: "Alert", message: "Your device cannot send emails", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)


        }
    }




func mailComposeController(controller: MFMailComposeViewController!,
    didFinishWithResult result: MFMailComposeResult,
    error: NSError!){

        switch(result.value){
        case MFMailComposeResultSent.value:
            println("Email sent")

        default:
            println("Whoops")
        }

        self.dismissViewControllerAnimated(true, completion: nil)

}
}

最佳答案

因为你没有将当前 View Controller 设置为 myMailmailComposeDelegate,所以 mailComposeController:didFinishWithResult 方法没有被调用.初始化 myMail 后,确保添加:

myMail.mailComposeDelegate = self

然后你就可以开始了

关于ios - 使用 Swift 实现应用内电子邮件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24255270/

相关文章:

iOS Soundcloud 登录问题

ios - 排序快速插入的 Realm 记录

c# - 通过电子邮件发送带有长名称和重音的附件

ios - xCode 7.1 中警报的 UITesting

android - 优步通用链接未设置下车位置

ios - JSON Post 方法无法快速运行

ios - 在 UITableviewcell swift 中设置水平渐变

ios - 在 Swift 中将 JSON 解析为 NSArray

java - 为什么我的电子邮件方法总是发送电子邮件并将电子邮件本身作为附件?

php - 如何在wordpress中发送带附件的邮件?