0

I trying to make a function that direct to WhatsApp with default text. In answer https://stackoverflow.com/a/61483365/14355301 it can work without the message or single word, how if I want to make the text a sentences? Once I make the text sentences, it cant work.

if let url = URL(string: "https://wa.me/\(item.contact!)?text=Testing test"), //cant work
UIApplication.shared.canOpenURL(url) {
   UIApplication.shared.open(url, options: [:])
}
Oxford212
  • 89
  • 1
  • 8

1 Answers1

0

You need to escape space (and other non-URL-compatible) characters in URL, like

URL(string: "https://wa.me/\(item.contact!)?text=Testing%20test")
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • How if I want to put some variable inside the text like `let t = "test"`. `URL(string: "https://wa.me/\(item.contact!)?text=Testing%20\(t)")`. – Oxford212 Nov 11 '20 at 06:20
  • yes you can, but then it is better to prepare separated `var text` variable combining everything needed for query text param (because that `t` also could have non-allowed characters) and then use `text.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)` – Asperi Nov 11 '20 at 06:39