【问题标题】:Open eMail from App with predefined text in iOS在 iOS 中使用预定义文本从 App 打开电子邮件
【发布时间】:2016-02-15 03:42:20
【问题描述】:

您好,我想从我的应用程序中打开电子邮件程序,并且应该已经定义了正文。我可以打开电子邮件,但不知道如何将电子邮件正文定义为给定参数以显示给定标准文本。任何人都可以帮忙吗?这是我用来打开电子邮件的代码:

//EMAIL
let email = "foo@bar.com"
let urlEMail = NSURL(string: "mailto:\(email)")

if UIApplication.sharedApplication().canOpenURL(urlEMail!) {
                UIApplication.sharedApplication().openURL(urlEMail!)
} else {
print("Ups")
}

【问题讨论】:

  • mailto: URL 方案进行一些研究。您可以提供“收件人”地址、“抄送”地址、主题和邮件正文。但当然,最好的选择是按照以下答案的建议进行操作。

标签: ios swift email url-scheme


【解决方案1】:

为 Xcode 12.5 更新

let url = NSURL(string: "mailto:mailto:someone@example.com")
                                            UIApplication.shared.open(url! as URL)

或者如果您想添加嵌入式主题

let url = NSURL(string: "mailto:someone@example.com?subject=This%20is%20the%20subject&cc=someone_else@example.com&body=This%20is%20the%20body")

或者如果您想添加多个电子邮件地址

let url = NSURL(string: "mailto:mailto:someone@example.com,someoneelse@example.com")
                                                UIApplication.shared.open(url! as URL)

【讨论】:

    【解决方案2】:

    Swift 5 版本的@mclaughj 答案

    let email = "foo@bar.com"
    let subject = "Your Subject"
    let body = "Plenty of email body."
                
    let coded = "mailto:\(email)?subject=\(subject)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        
    if let emailURL:NSURL = NSURL(string: coded!)
           {
              if UIApplication.shared.canOpenURL(emailURL as URL){
                 UIApplication.shared.open(emailURL as URL)
              }
           }
    

    干杯!

    【讨论】:

      【解决方案3】:

      与其他答案中的 url 构造类似,但您可以使用 URLComponents,而不是调用 addingPercentEncoding

      var components = URLComponents(string: "youremail@test.com")
      components?.queryItems = [URLQueryItem(name: "subject", value: "Your Subject")]
      
      if let mailUrl = components?.url {
          UIApplication.shared.open(mailUrl, options: [:], completionHandler: nil)
      }
      

      【讨论】:

        【解决方案4】:

        您可以使用MFMailComposeViewController

        import MessageUI
        
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self
        mailComposerVC.setToRecipients(["email@email.com"])
        mailComposerVC.setSubject("Subject")
        mailComposerVC.setMessageBody("Body", isHTML: false)
        self.presentViewController(mailComposerVC, animated: true, completion: nil)
        

        此外,您需要从 MFMailComposeViewControllerDelegate 实现 mailComposeController:didFinishWithResult:error:,您应该在其中关闭 MFMailComposeViewController

        【讨论】:

        • 在创建 mailComposerVC 之前,检查您是否真的可以使用 MFMailComposeViewController.canSendMail() 发送电子邮件是一个不错的主意
        【解决方案5】:

        Swift 4.0

            let email = "feedback@company.com"
            let subject = "subject"
            let bodyText = "Please provide information that will help us to serve you better"
            if MFMailComposeViewController.canSendMail() {
                let mailComposerVC = MFMailComposeViewController()
                mailComposerVC.mailComposeDelegate = self
                mailComposerVC.setToRecipients([email])
                mailComposerVC.setSubject(subject)
                mailComposerVC.setMessageBody(bodyText, isHTML: true)
                self.present(mailComposerVC, animated: true, completion: nil)
            } else {
                let coded = "mailto:\(email)?subject=\(subject)&body=\(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
                if let emailURL = URL(string: coded!)
                {
                    if UIApplication.shared.canOpenURL(emailURL)
                    {
                        UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in
                            if !result {
                                // show some Toast or error alert
                                //("Your device is not currently configured to send mail.")
                            }
                        })
                    }
                }
            }
        

        【讨论】:

          【解决方案6】:

          Swift 3 版本

          let subject = "Some subject"
          let body = "Plenty of email body."
          let coded = "mailto:blah@blah.com?subject=\(subject)&body=\(body)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
          
          if let emailURL: NSURL = NSURL(string: coded!) {
              if UIApplication.shared.canOpenURL(emailURL as URL) {
                  UIApplication.shared.openURL(emailURL as URL)
              }
          }
          

          【讨论】:

            【解决方案7】:

            如果您想打开内置电子邮件应用程序而不是像其他人提到的那样显示MFMailComposeViewController,您可以像这样构建mailto: 链接:

            let subject = "My subject"
            let body = "The awesome body of my email."
            let encodedParams = "subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
            let url = "mailto:foo@bar.com?\(encodedParams)"
            
            if let emailURL = NSURL(url) {
                if UIApplication.sharedApplication().canOpenURL(emailURL) {
                    UIApplication.sharedApplication().openURL(emailURL)
                }
            }
            

            为了节省任何人的打字时间,2016 年的语法略有变化:

            let subject = "Some subject"
            let body = "Plenty of email body."
            let coded = "mailto:blah@blah.com?subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
            
            if let emailURL:NSURL = NSURL(string: coded!)
                {
                if UIApplication.sharedApplication().canOpenURL(emailURL)
                    {
                    UIApplication.sharedApplication().openURL(emailURL)
                    }
            

            【讨论】:

            • 页面上问题的实际正确答案!谢啦! :)
            【解决方案8】:

            我建议使用Apple的方式,您可以在MFMailComposeViewController的官方文档中找到。它使用电子邮件打开一个模态视图控制器,发送后将其关闭。因此,用户会留在您的应用中。

            【讨论】:

            • OP 要求提供 Swift 代码,您的参考显示的是 Obj-C。
            • 该链接指向 Apple 的文档,其中包含 Jan 可能需要的所有信息,而不仅仅是特定部分(方法/属性)。在这种情况下,Apple 仅在 ObjC 中提供了示例代码,但它可以很容易地转换为 Swift。
            【解决方案9】:

            像这样使用MFMailComposeViewController

            1. 导入 MessageUI

              import MessageUI
              
            2. 将代理添加到您的班级:

              class myClass: UIViewController, MFMailComposeViewControllerDelegate {}
              
            3. 配置您想要的电子邮件预设

              let mail = MFMailComposeViewController()
              mail.mailComposeDelegate = self
              mail.setSubject("Subject")
              mail.setMessageBody("Body", isHTML: true)
              mail.setToRecipients(["my@email.com"])
              presentViewController(mail, animated: true, completion: nil)
              
            4. 将此方法放入您的代码中:

              func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
                  dismissViewControllerAnimated(true, completion: nil)
              }
              

            你去,现在可以工作了。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-08-30
              • 2013-12-04
              • 2012-06-28
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多