Sample Code:
let sampleURL = "http://Hello/site/link?id=MTk=&fid=MTA="
let urlWhats = "whatsapp://send?text=\(sampleURL)"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
UIApplication.shared.open(whatsappURL as URL)
}
else {
// Open App Store.
}
}
}
added in .plist file
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
Facing issue is while sharing link with & text not coming after &
For above url text is coming like http://Hello/site/link?id=MTk=
Thanks in advance
Different parts of an URL have different encoding rules. addingPercentEncoding
is very tricky to use correctly. As a rule, you should not try to encode your own URLs. Let the system do it for you with URLComponents:
//For the constant part of the link, it's fine to just use a string
var sample = URLComponents(string: "whatsapp://send")!
// Then add a query item
sample.queryItems = [URLQueryItem(name: "text", value: "http://Hello/site/link?id=MTk=&fid=MTA=")]
// Extract the URL, which will have the correct encoding
print(sample.url!)
// whatsapp://send?text=http://Hello/site/link?id%3DMTk%3D%26fid%3DMTA%3D