这是一个示例代码:
当我按下按钮时,函数showEmail被执行并显示发送电子邮件的表单。如果我点击“发送”按钮,那么一切都正常——邮件被发送,随后执行函数mailComposeController。NSLog显示标签“Mail sent”,然后初始屏幕重新出现。
如果我在发送邮件的对话框中,点击“取消”按钮,则对话框不会消失,函数 mailComposeController 不起作用,“发送”和“取消”两个按钮变为灰色,一直保持这样。
问题出在哪里?
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 = ["a.nakhimov@gmail.com"]
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)
}
}
当我按下按钮时,函数showEmail被执行并显示发送电子邮件的表单。如果我点击“发送”按钮,那么一切都正常——邮件被发送,随后执行函数mailComposeController。NSLog显示标签“Mail sent”,然后初始屏幕重新出现。
如果我在发送邮件的对话框中,点击“取消”按钮,则对话框不会消失,函数 mailComposeController 不起作用,“发送”和“取消”两个按钮变为灰色,一直保持这样。
问题出在哪里?