1 year ago

#353784

test-img

NullPointerException

How to use mailto: with url encoded string

I need to manage mailto protocol with this string:

mailto:sales@myweb.co.uk?subject=Something%20from%20Random%20Thing%20app

I'm doing with this code:

if parameter.hasPrefix("mailto") {
    let urlString = parameter.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    if let url = URL(string: urlString ?? ""), UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url)
    } else {
        let alert = UIAlertController(title: L10n.error, message: L10n.notSupportEmail, preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alert.addAction(cancelAction)
        SectionManager.sharedInstance.navigationController?.topViewController?.present(alert, animated: true, completion: nil)
    }
}

When the mail app is opened, the subject still contains the "%20" encoded spaces.

How can this be avoided?

ios

swift

urlencode

mailto

0 Answers

Your Answer

Accepted video resources