我正在尝试使用发送电子邮件选项设置应用。
我有这段代码:
import Foundation
import MessageUI
import UIKit
class emailClass: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if !MFMailComposeViewController.canSendMail() {
print("Mail services are not available")
return
}
sendEmail()
}
func sendEmail() {
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["address@example.com"])
composeVC.setSubject("Hello!")
composeVC.setMessageBody("Hello this is my message body!", isHTML: false)
// Present the view controller modally.
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)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
所以我收到此消息:"邮件服务不可用"。 现在我已经登录了iCloud中的模拟器设备...所以我认为它应该这样做,但事实并非如此。为什么这不起作用?你能告诉我什么是错的,我怎么能继续前进?
答案 0 :(得分:41)
它不适用于模拟器。请在iPhone设备上测试。您可以参考Apple Developer Portal - MFMailComposeViewController
答案 1 :(得分:29)
这是我的表现方式。看起来你很好地遵循了文档,我想我会添加我的变体,以防它帮助其他人。另外,这是对当前(2017年8月)语法的更新。
遵守MFMailComposeViewControllerDelegate协议,并检查设备是否可以发送邮件。
import Foundation
import UIKit
import MessageUI
class WelcomeViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if !MFMailComposeViewController.canSendMail() {
print("Mail services are not available")
return
}
}
我的应用使用IBAction启动邮件撰写。
@IBAction func sendFeedbackButtonTapped(_ sender: Any) {
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["exampleEmail@email.com"])
composeVC.setSubject("Message Subject")
composeVC.setMessageBody("Message content.", isHTML: false)
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)
}
关于以下mailComposeController函数,文档说
邮件撰写视图控制器不会自动解除。当用户点击按钮发送电子邮件或取消界面时,邮件撰写视图控制器会调用 mailComposeController(_:didFinishWith:错误:) 其代表的方法。您对该方法的实现必须显式地关闭视图控制器,如清单3所示。您还可以使用此方法来检查操作的结果。
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult, error: Error?) {
// Check the result or perform other tasks.
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
}
}
答案 2 :(得分:8)
代码似乎很好,如果应用程序在真实设备中运行,则可以正常运行
{{1}}
您无法在模拟器上对其进行测试,您将能够测试基本内容,例如用户界面,取消/发送按钮点击时发生的事情。
要测试,您必须使用设备,设备中的Mail应用程序应配置一些邮件(例如:abc@xyz.com)。
希望有所帮助。
答案 3 :(得分:1)
在 Swift 5 中发送电子邮件很容易,您需要确认并实现MFMailComposeViewControllerDelegate并检查我们是否可以在此设备上发送电子邮件
这是我用于执行任务的一小段代码
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: IBAction Method for Button click
@IBAction func sendEmail(_ sender: Any) {
//TODO: You should chack if we can send email or not
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["you@yoursite.com"])
mail.setSubject("Email Subject Here")
mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)
present(mail, animated: true)
} else {
print("Application is not able to send an email")
}
}
//MARK: MFMail Compose ViewController Delegate method
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}
}
PS:请不要忘记您需要在真实设备上进行测试
答案 4 :(得分:0)
您的代码存在的问题是
//以模态方式呈现视图控制器。 self.present(composeVC,animated:true,completion:nil)
本身就存在; - )
如果你不知道当前的视图控制器,只需在RootViewController上显示它,即
UIApplication.shared.keyWindow?.rootViewController?.present(...