如何在swiftui邮件应用程序中传递主题和正文文本?

问题描述

我试图在打开邮件应用程序时传递主题和正文文本,但是由于某些原因,当我传递参数时邮件不会发生。谁能在我的代码中看到我做错了什么?或者,如果有其他方法让我在swiftui中工作,那真是太棒了。感谢您的帮助。

这是我的代码:

Button(action: {
                         
     let email = "test@gmail.com"
     let subject = "Feedback"
    let body = "Please provide your feedback here,and we will contact you within the next 24-48 hours."
   guard let url = URL(string: "mailto:\(email)?subject=\(subject)&body=\(body)") else { return }

   UIApplication.shared.open(url)
}) {

  Text("Contact Us - ")
 }

解决方法

通常,您需要为组成URL的组件添加转义百分比,例如

     let email = "test@gmail.com"
     let subject = "Feedback"
    let body = "Please provide your feedback here,and we will contact you within the next 24-48 hours."
   guard let url = URL(string: "mailto:\(email)?subject=\(subject.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "")&body=\(body.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "")") else { return }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...