如何从Swift打开邮件应用程序
我正在使用一个简单的快速应用程序,用户input一个电子邮件地址,并按下一个button,打开邮件应用程序,input地址在地址栏中。 我知道如何在Objective-C中做到这一点,但我很难让它在Swift中工作。
您可以在iOS中使用简单的mailto:链接来打开邮件应用程序。
let email = "foo@bar.com" if let url = URL(string: "mailto:\(email)") { UIApplication.shared.open(url) }
我不确定是否要切换到邮件应用程序本身,或只是打开并发送电子邮件。 对于链接到buttonIBAction的后一个选项:
import UIKit import MessageUI class ViewController: UIViewController, MFMailComposeViewControllerDelegate { @IBAction func launchEmail(sender: AnyObject) { var emailTitle = "Feedback" var messageBody = "Feature request or bug report?" var toRecipents = ["friend@stackoverflow.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) } func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) { switch result { case MFMailComposeResultCancelled: print("Mail cancelled") case MFMailComposeResultSaved: print("Mail saved") case MFMailComposeResultSent: print("Mail sent") case MFMailComposeResultFailed: print("Mail sent failure: \(error?.localizedDescription)") default: break } self.dismissViewControllerAnimated(true, completion: nil) } }
Swift 2, 可用性检查:
import MessageUI if MFMailComposeViewController.canSendMail() { let mail = MFMailComposeViewController() mail.mailComposeDelegate = self mail.setToRecipients(["test@test.test"]) mail.setSubject("Bla") mail.setMessageBody("<b>Blabla</b>", isHTML: true) presentViewController(mail, animated: true, completion: nil) } else { print("Cannot send mail") // give feedback to the user } // MARK: - MFMailComposeViewControllerDelegate func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { switch result.rawValue { case MFMailComposeResultCancelled.rawValue: print("Cancelled") case MFMailComposeResultSaved.rawValue: print("Saved") case MFMailComposeResultSent.rawValue: print("Sent") case MFMailComposeResultFailed.rawValue: print("Error: \(error?.localizedDescription)") default: break } controller.dismissViewControllerAnimated(true, completion: nil) }
在Swift 3中,确保添加和导入MessageUI并需要符合MFMailComposeViewControllerDelegate协议**
func sendEmail() { if MFMailComposeViewController.canSendMail() { let mail = MFMailComposeViewController() mail.mailComposeDelegate = self mail.setToRecipients(["ved.ios@yopmail.com"]) mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true) present(mail, animated: true) } else { // show failure alert } } Protocol func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { controller.dismiss(animated: true) }
斯威夫特·斯威夫特更新了斯威夫特3的答案
let email = "email@email.com" let url = URL(string: "mailto:\(email)") UIApplication.shared.openURL(url!)
这是Swift中3个步骤的直接解决scheme。
import MessageUI
添加以符合委托
MFMailComposeViewControllerDelegate
只要创build你的方法:
func sendEmail() { if MFMailComposeViewController.canSendMail() { let mail = MFMailComposeViewController() mail.mailComposeDelegate = self mail.setToRecipients(["support@mail.com"]) mail.setSubject("Support App") mail.setMessageBody("<p>Send us your issue!</p>", isHTML: true) presentViewController(mail, animated: true, completion: nil) } else { // show failure alert } } func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { controller.dismissViewControllerAnimated(true, completion: nil) }
import UIKit import MessageUI class ViewController: ViewController,MFMailComposeViewControllerDelegate { @IBAction func EmailBtnClicked(sender: AnyObject) { let mailComposeViewController = configuredMailComposeViewController() if MFMailComposeViewController.canSendMail() { self.presentViewController(mailComposeViewController, animated: true, completion: nil) } else { self.showSendMailErrorAlert() } } func configuredMailComposeViewController() -> MFMailComposeViewController { let mailComposerVC = MFMailComposeViewController() mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property mailComposerVC.setToRecipients(["ved.ios@gmail.com"]) mailComposerVC.setSubject("Sending you an in-app e-mail...") mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad in swift!", isHTML: false) return mailComposerVC } func showSendMailErrorAlert() { let sendMailErrorAlert = UIAlertView(title: "Unable to Send Email", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK") sendMailErrorAlert.show() } // MARK: MFMailComposeViewControllerDelegate func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) { controller.dismissViewControllerAnimated(true, completion: nil) } }
@IBAction func launchEmail(sender: AnyObject) { if if MFMailComposeViewController.canSendMail() { var emailTitle = "Feedback" var messageBody = "Feature request or bug report?" var toRecipents = ["friend@stackoverflow.com"] var mc: MFMailComposeViewController = MFMailComposeViewController() mc.mailComposeDelegate = self mc.setSubject(emailTitle) mc.setMessageBody(messageBody, isHTML: false) mc.setToRecipients(toRecipents) self.present(mc, animated: true, completion: nil) } else { // show failure alert } } func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) { switch result { case .cancelled: print("Mail cancelled") case .saved: print("Mail saved") case .sent: print("Mail sent") case .failed: print("Mail sent failure: \(error?.localizedDescription)") default: break } self.dismiss(animated: true, completion: nil) }
请注意,并非所有用户都将其设备configuration为发送电子邮件,这就是为什么我们需要在尝试发送前检查canSendMail()的结果。 还要注意,你需要赶上didFinishWithcallback,以消除邮件窗口。
这里如何找Swift 4:
import MessageUI if MFMailComposeViewController.canSendMail() { let mail = MFMailComposeViewController() mail.mailComposeDelegate = self mail.setToRecipients(["test@test.test"]) mail.setSubject("Bla") mail.setMessageBody("<b>Blabla</b>", isHTML: true) present(mail, animated: true, completion: nil) } else { print("Cannot send mail") // give feedback to the user } func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { switch result.rawValue { case MFMailComposeResult.cancelled.rawValue: print("Cancelled") case MFMailComposeResult.saved.rawValue: print("Saved") case MFMailComposeResult.sent.rawValue: print("Sent") case MFMailComposeResult.failed.rawValue: print("Error: \(String(describing: error?.localizedDescription))") default: break } controller.dismiss(animated: true, completion: nil) }
对于我们这些仍然落后于Swift 2.3的人来说,这里是Gordon在语法上的回答:
let email = "foo@bar.com" if let url = NSURL(string: "mailto:\(email)") { UIApplication.sharedApplication().openURL(url) }