MFMailComposeViewController.canSendMail()为true,gmail屏幕显示正确的收件人电子邮件ID,主题和正文。但是发送按钮被禁用。此外,当我按下取消按钮滑块时,会出现两个选项:删除和取消。删除不起作用,我无法从邮件编辑器返回到我以前的控制器。
Gmail已在我的iphone上设置并且运行正常。我无法弄清楚出了什么问题,有人可以帮忙吗?
MFMailComposeViewController.canSendMail() {
if (selectedEntry!.notifyType == "Email") {
let mailComposeVC = MFMailComposeViewController()
mailComposeVC.mailComposeDelegate = self
let target:String = (selectedEntry?.targetString)!
mailComposeVC.setToRecipients([target])
mailComposeVC.setSubject("Test")
mailComposeVC.setMessageBody("Hello ", isHTML: false)
// present the message view controller
self.present(mailComposeVC, animated: true, completion: nil)
}
} else {
print("MAIL services are not available")
return
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
{
switch (result)
{
case .sent:
print("email sent.")
break
case .cancelled:
print("email cancelled.")
break
case .failed:
print("failed sending email")
break
default:
break
}
controller.dismiss(animated: true, completion: nil)
}
答案 0 :(得分:0)
很遗憾,MFMailComposeViewController
由本机邮件应用程序提供支持。取自Apple documentation:
如果用户选择发送邮件,则该邮件会在 用户的邮件应用发件箱。邮件应用最终负责 发送消息。
由于在设备上设置了Gmail帐户,因此即使从设备“卸载”了邮件应用,canSendMail
也会返回true。该应用程序并未真正卸载,而是隐藏了。如果您尝试以以下方式检查该应用程序是否存在,则会收到误报。
let mailURL = URL(string: "mailto:?to=\(emailAddress)")!
let canSendEmail = UIApplication.shared.canOpenURL(mailURL)
使用UIApplication.shared.open(mailURL)
打开经过验证的网址将显示一条警报,询问您是否要重新安装该应用程序。
根据this response,此问题仅在iOS 12或更高版本中存在。
答案 1 :(得分:0)
与 Swift 5 配合良好。
首先,对用于发送电子邮件的 viewController 进行扩展:
import UIKit
import Foundation
import MessageUI
import LocalAuthentication
extension viewControllerThatSendsTheEmail: MFMailComposeViewControllerDelegate
{
func mailComposeController( _ controller: MFMailComposeViewController, didFinishWith result:MFMailComposeResult, error: Error?)
{
if let _ = error
{
controller.dismiss(animated: true)
return
}
switch result
{
case.cancelled:
print("CANCELLED")
case.failed:
print("FAILED")
case.saved:
print("SAVED")
case.sent:
print("EMAIL SENT")
@unknown default:
fatalError()
}
controller.dismiss(animated: true)
}
}
那么你的函数:
@objc func sendEmailButtonTap()
{
guard MFMailComposeViewController.canSendMail()
else
{
return
}
let sending = MFMailComposeViewController()
sending.mailComposeDelegate = self
sending.setToRecipients(["awesomeDeveloper@apple.com"])
sending.setSubject("AWRSOME APP")
sending.setMessageBody("THIS APP IS SO AWESOME!", isHTML: false)
present(sending, animated: true)
}