UIButton 没有调出 MFMailViewController
Posted
技术标签:
【中文标题】UIButton 没有调出 MFMailViewController【英文标题】:UIButton does not bring up MFMailViewController 【发布时间】:2017-02-21 22:23:59 【问题描述】:我遇到了一些代码错误。我对此很陌生,我正在努力自学。我在网上找到了大部分答案,但似乎找不到关于这个特定问题的任何信息。我想在应用程序中发送一封电子邮件,但只要我按下电子邮件按钮,MFMailViewController 就不会出现。就像我的 UIButton 不工作一样。但我知道我有它作为 IBAction。到目前为止,这是我的代码。非常感谢任何帮助。
import UIKit
import MessageUI
class RequestService: UIViewController,MFMailComposeViewControllerDelegate
@IBOutlet weak var CustomerName: UITextField!
@IBOutlet weak var emailButton: UIButton!
@IBAction func sendEmail(_ sender: UIButton)
if !MFMailComposeViewController.canSendMail()
print("Mail services are not available")
let ComposeVC = MFMailComposeViewController()
ComposeVC.mailComposeDelegate = self
ComposeVC.setToRecipients(["jwelch@ussunsolar.com"])
ComposeVC.setSubject("New Support Ticket")
ComposeVC.setMessageBody(CustomerName.text!, ishtml: false)
self.present(ComposeVC, animated: true, completion: nil)
func mailComposeController(controller: MFMailComposeViewController,didFinishWithResult result:MFMailComposeResult, error: NSError?)
// Check the result or perform other tasks.
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
【问题讨论】:
你在模拟器中测试这个吗?这仅适用于实际的 iPhone 设备。 【参考方案1】:您在 sendMail
函数中的语法有误。您发布的代码只会在设备无法发送邮件时打开视图控制器。改成这样:
@IBAction func sendEmail(_ sender: UIButton)
if !MFMailComposeViewController.canSendMail()
print("Mail services are not available")
return
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
composeVC.setToRecipients(["jwelch@ussunsolar.com"])
composeVC.setSubject("New Support Ticket")
composeVC.setMessageBody(CustomerName.text!, isHTML: false)
self.present(composeVC, animated: true, completion: nil)
【讨论】:
邮件发不出去,为什么还要继续展示邮件编写器? 你们知道你们的东西!我只是改变了你所说的,它完美无缺!【参考方案2】:只有在设备无法发送电子邮件时,您才尝试显示邮件控制器。那是倒退。
@IBAction func sendEmail(_ sender: UIButton)
if MFMailComposeViewController.canSendMail()
print("Mail services are not available")
let ComposeVC = MFMailComposeViewController()
ComposeVC.mailComposeDelegate = self
ComposeVC.setToRecipients(["jwelch@ussunsolar.com"])
ComposeVC.setSubject("New Support Ticket")
ComposeVC.setMessageBody(CustomerName.text!, isHTML: false)
self.present(ComposeVC, animated: true, completion: nil)
func mailComposeController(controller: MFMailComposeViewController,didFinishWithResult result:MFMailComposeResult, error: NSError?)
// Check the result or perform other tasks.
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
您需要在其他方法之外使用委托方法。
【讨论】:
以上是关于UIButton 没有调出 MFMailViewController的主要内容,如果未能解决你的问题,请参考以下文章