2

I am trying to pass in subject and body text when I open the mail app but for some reason the mail wont happen hen I pass in parameters . can anyone see in my code of what I am doing wrong? or if guys have another method for me to work in swiftui that will be awesome. thanks for the help.

here is my code:

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 - ")
 }

1 Answers1

5

In general you need to add percent escaping for components of composed URL, like

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 }
herve
  • 3,825
  • 2
  • 18
  • 27
Asperi
  • 228,894
  • 20
  • 464
  • 690