I have a string that can include any characters, from alphanumeric to special chars like "&,.:/!" etc. I need to encode that string and send it to twitter. I tried the following but as soon as theres an &
character in text
, it doesn't work properly:
static func shareToTwitter(text: String) {
guard let urlEscaped = text.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
return
}
guard let url = URL(string: "twitter://post?message=\(urlEscaped)") else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
} else {
guard let twitterUrl = URL(string: "https://twitter.com/intent/tweet?text=\(urlEscaped)") else {
return
}
UIApplication.shared.open(twitterUrl)
}
}
So an example sentence might be: "I'm here & there". Instead, Twitter will receive "I'm here". How can I fix this?