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

Sco*_*ank 2 uiapplication ios swift 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 - ")
 }
Run Code Online (Sandbox Code Playgroud)

Asp*_*eri 6

一般来说,您需要为组合 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 }
Run Code Online (Sandbox Code Playgroud)