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

how to pass in subject and body text in swiftui mail app?

我试图在打开邮件应用程序时传递主题和正文文本,但由于某种原因,当我传递参数时邮件不会发生。谁能在我的代码中看到我做错了什么?或者,如果大家有另一种方法让我在 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 }