如何使用Swfit在ios上启动电子邮件客户端

如何使用Swfit在ios上启动电子邮件客户端,ios,swift,Ios,Swift,在安卓系统中,我可以使用其意图机制从安卓应用程序启动电子邮件客户端。 在ios中,如何使用Swift从ios应用程序启动其电子邮件客户端 多谢各位 let url = NSURL(string: "mailto:jon.doe@mail.com") UIApplication.sharedApplication().openURL(url) 请注意,这只适用于设备,而不适用于模拟器。我从Paul Hudson那里找到了一个很好的解决方案。在Swift 3中,将import MessageUI添

在安卓系统中,我可以使用其意图机制从安卓应用程序启动电子邮件客户端。 在ios中,如何使用Swift从ios应用程序启动其电子邮件客户端

多谢各位

let url = NSURL(string: "mailto:jon.doe@mail.com")
UIApplication.sharedApplication().openURL(url)

请注意,这只适用于设备,而不适用于模拟器。

我从Paul Hudson那里找到了一个很好的解决方案。在Swift 3中,将
import MessageUI
添加到文件顶部,并使类符合
MFMailComposeViewControllerDelegate
协议

func sendEmail() {
  if MFMailComposeViewController.canSendMail() {
   let mail = MFMailComposeViewController()
   mail.mailComposeDelegate = self
   mail.setToRecipients(["example@example.com"])
   mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)

   present(mail, animated: true)
   } else {
   // show failure alert
  }
}

// MARK: MFMailComposeViewControllerDelegate Conformance

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    controller.dismiss(animated: true)
}
func sendmail(){
如果MFMailComposeViewController.canSendMail(){
让mail=MFMailComposeViewController()
mail.mailComposeDelegate=self
mail.setToRecipients([”example@example.com"])
setMessageBody(“你太棒了!

”,isHTML:true) 当前(邮件,动画:真) }否则{ //显示故障警报 } } //标记:MFMAILCOMPOSEVIEWCONTROLLEDELEGATE符合性 func-mailcomosecontroller(u控制器:mfmailcomoseviewcontroller,didFinishWith结果:MFMailComposeResult,错误:error?){ 控制器。解除(动画:真) }
SWIFT 3:如果iOS邮件应用可用,openEmail功能将尝试使用它(用户至少有一个电子邮件帐户设置)。否则,它将使用mailto:url(请参阅回复底部的完整示例)启动邮件客户端

import MessageUI
// Make your view controller conform to MFMailComposeViewControllerDelegate
class Foo: UIViewController, MFMailComposeViewControllerDelegate {

    func openEmail(_ emailAddress: String) {
        // If user has not setup any email account in the iOS Mail app
        if !MFMailComposeViewController.canSendMail() {
            print("Mail services are not available")
            let url = URL(string: "mailto:" + emailAddress)
            UIApplication.shared.openURL(url!)
            return
        }

        // Use the iOS Mail app
        let composeVC = MFMailComposeViewController()
        composeVC.mailComposeDelegate = self
        composeVC.setToRecipients([emailAddress])
        composeVC.setSubject("")
        composeVC.setMessageBody("", isHTML: false)

        // Present the view controller modally.
        self.present(composeVC, animated: true, completion: nil)
    }

    // MARK: MailComposeViewControllerDelegate
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        // Dismiss the mail compose view controller.
        controller.dismiss(animated: true, completion: nil)
    }
}
一个包含主题、正文和抄送的完整mailto示例:

“邮寄:me@gmail.com主题=嘿,怎么了,伙计!&body=这是一个 我在网上找到了一个很好的例子。&cc=someoneelse@yahooo.com&密件抄送=whostillusesthis@hotmail.com"


更新iOS 10+

//用于打开邮件应用程序或使用浏览器打开任何链接

let url = NSURL(string: "mailto:your_mail_here@mail.com")

if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
   // Fallback on earlier versions
   UIApplication.shared.openURL(url)
}

你想发送邮件或启动应用程序吗?谢谢。我可以在启动电子邮件应用程序时添加“主题”和“正文”吗?可以,您可以使用url方案。有关说明,请参阅。甚至还有一个啊,是的,别忘了勾选上面的复选标记。一旦邮件应用程序启动,点击邮件应用程序视图上的“取消”按钮会导致应用程序崩溃,有解决方案吗?我还尝试了MFComposeViewController,但得到了完全相同的结果