ios – 使用Swift实现应用内邮件失败

我想使用 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 = ["[email protected]"]
            myMail.setToRecipients(toRecipients)

            //CC recipients
            var ccRecipients = ["[email protected]"]
            myMail.setCcRecipients(ccRecipients)

            //CC recipients
            var bccRecipients = ["[email protected]"]
            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,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)

}
}

解决方法

由于您尚未将当前视图控制器设置为myMail的mailComposeDelegate,因此未调用mailComposeController:didFinishWithResult方法.初始化myMail后,请确保添加:
myMail.mailComposeDelegate = self

你会很好的去

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...