-1

Consider:

let body =  "Some text: www.google.co.in?q=1&q2=sdsdsds"
let urlString = "mailto:\(toEmail)?subject=\(subject.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!)&body=\(body.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!)"

Whenever the mail client opens, it strips the second parameter &q2=sdsdsds.. How can I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hemant Bavle
  • 3,297
  • 1
  • 24
  • 30

1 Answers1

0

The proper way is to use URLComponents. Here is some code:

var urlComponents = URLComponents(string: "your URL string without queries")
urlComponents?.queryItems = [URLQueryItem(name: "subject", value: subject)]

Also checkout this article by Sundell.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
  • Can you show me a working example using the above? I tried, the ref link but it didn't work. – Hemant Bavle May 23 '20 at 19:10
  • Did you check-out the documentation or sundell's article link I provided? I've used it everywhere, also have my own library for service calls which use `urlComponents` effectively. – Frankenstein May 23 '20 at 19:30
  • @HemantBavle Also note, url's can't contain spaces, I hope you know this should be urlEncoded to `%20` here is the link for that: https://stackoverflow.com/questions/24551816/swift-encode-url – Frankenstein May 23 '20 at 19:32
  • I have tried all of encoding stuff, nothing works, when the email client opens, I do not see the query params except the one after question mark. – Hemant Bavle May 23 '20 at 19:37