本文介绍了Swift中的MFMailComposeViewController不会被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是示例代码:

  import UIKit 
import MessageUI

class ViewController :UIViewController,MFMailComposeViewControllerDelegate {

@IBAction func showEmail(sender:AnyObject){
var emailTitle =Test Email
var messageBody =这是一个测试邮件正文
var toRecipents = [[email protected]]
var mc:MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody,isHTML:false)
mc.setToRecipients(toRecipents)

self.presentViewController(mc,animated:true,completion:nil)
}

覆盖func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从笔尖进行任何其他设置。
}

覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理可以重新创建的任何资源。
}

func mailComposeController(控制器:MFMailComposeViewController,didFinishWithResult结果:MFMailComposeResult,错误:NSError){
switch result.value {
case MFMailComposeResultCancelled.value:
NSLog(邮件已取消)
case MFMailComposeResultSaved.value:
NSLog(邮件保存)
case MFMailComposeResultSent.value:
NSLog(邮件发送)
case MFMailComposeResultFailed.value:
NSLog(邮件发送失败:%@,[error.localizedDescription])
默认值:
break
}
self .dismissModalViewControllerAnimated(true)
// self.dismissViewControllerAnimated(true,completion:nil)
}

}

当我按下按钮时,执行功能 showEmail 并显示发送电子邮件的表单。如果我点击发送,那么一切正常 - 发送邮件,然后执行函数 mailComposeController 。 NSLog显示标签Mail sent,初始屏幕重新出现。



如果我在发送邮件对话框中,请单击取消按钮,然后对话框不会消失,功能 mailComposeController 不起作用,两个按钮 - 发送和取消,变为灰色,因此保持不变。 / p>

有什么问题?

解决方案

似乎是iOS 8中的错误。 Objective-C中也存在同样的问题。


This is sample code:

import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

@IBAction func showEmail(sender : AnyObject) {
    var emailTitle = "Test Email"
    var messageBody = "This is a test email body"
    var toRecipents = ["[email protected]"]
    var mc: MFMailComposeViewController = MFMailComposeViewController()
    mc.mailComposeDelegate = self
    mc.setSubject(emailTitle)
    mc.setMessageBody(messageBody, isHTML: false)
    mc.setToRecipients(toRecipents)

    self.presentViewController(mc, animated: true, completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
    switch result.value {
    case MFMailComposeResultCancelled.value:
        NSLog("Mail cancelled")
    case MFMailComposeResultSaved.value:
        NSLog("Mail saved")
    case MFMailComposeResultSent.value:
        NSLog("Mail sent")
    case MFMailComposeResultFailed.value:
        NSLog("Mail sent failure: %@", [error.localizedDescription])
    default:
        break
    }
    self.dismissModalViewControllerAnimated(true)
    // self.dismissViewControllerAnimated(true, completion: nil)
}

}

When I push the button, the function showEmail is executed and appears the form for send email. If I click "Send", then everything works fine - mail is sent, then the function mailComposeController is executed. NSLog displays the label "Mail sent" and the initial screen reappears.

If I'm in the dialog box of send mail, click "Cancel" button, then dialogue does not disappear, function mailComposeController does not work, two buttons - "Send" and "Cancel", becoming gray color and so it stays.

What wrong?

解决方案

Seems to be bug in iOS 8. Same problem exist in Objective-C also.

这篇关于Swift中的MFMailComposeViewController不会被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 20:56