func openMail(emailTo:String, subject: String, body: String) {
if let url = URL(string: "mailto:\(emailTo)?subject=\(subject.fixToBrowserString())&body=\(body.fixToBrowserString())"),
UIApplication.shared.canOpenURL(url)
{
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
extension String {
func fixToBrowserString() -> String {
self.replacingOccurrences(of: ";", with: "%3B")
.replacingOccurrences(of: "\n", with: "%0D%0A")
.replacingOccurrences(of: " ", with: "+")
.replacingOccurrences(of: "!", with: "%21")
.replacingOccurrences(of: "\"", with: "%22")
.replacingOccurrences(of: "\\", with: "%5C")
.replacingOccurrences(of: "/", with: "%2F")
.replacingOccurrences(of: "‘", with: "%91")
.replacingOccurrences(of: ",", with: "%2C")
//more symbols fixes here: https://mykindred.com/htmlspecialchars.php
}
}
if you have some issues with opening of the email link - probably you need to add addidtional fixes to function fixToBrowserString()
usage in SwiftUI:
Button("Support Email") {
openMail(emailTo: "support@gmail.com",
subject: "App feedback",
body: "Huston, we have a problem!\n\n...")
}