使用Swift实现应用程序内电子邮件失败 [英] Failed on using Swift to implement in-app email

查看:151
本文介绍了使用Swift实现应用程序内电子邮件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use swift to implement in-app email. When I click the button, the email window pops up. However, I am unable to send my email. Moreover, after I click cancel-delete draft, I cannot go back to the original screen.

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)

}
}


推荐答案

由于您尚未将当前视图控制器设置为 mailComposeDelegate myMail ,未调用 mailComposeController:didFinishWithResult 方法。初始化 myMail 后,请务必添加:

Since you haven't set the current view controller as the mailComposeDelegate of myMail, the mailComposeController:didFinishWithResult method isn't being called. After you init myMail, make sure to add:

myMail.mailComposeDelegate = self

你会很高兴

这篇关于使用Swift实现应用程序内电子邮件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆