With the new option of setting the default mail app on settings, I'm wondering if there is a simpler solution to sending emails. Right now I'm using a manual solution adding 3rd party apps manually, like this:
...
if MFMailComposeViewController.canSendMail() {
self.settingsViewModel.showingFeatureEmail.toggle()
} else if let emailUrl = self.settingsViewModel.createEmailUrl(to: self.generalConstants.email, subject: "Feature request! [\(self.generalConstants.appName)]", body: "Hello, I have this idea ") {
UIApplication.shared.open(emailUrl)
} else {
self.settingsViewModel.showMailFeatureAlert = true
}
...
func createEmailUrl(to: String, subject: String, body: String) -> URL? {
let subjectEncoded = subject.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
let bodyEncoded = body.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
let gmailUrl = URL(string: "googlegmail://co?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
let outlookUrl = URL(string: "ms-outlook://compose?to=\(to)&subject=\(subjectEncoded)")
let yahooMail = URL(string: "ymail://mail/compose?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
let sparkUrl = URL(string: "readdle-spark://compose?recipient=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
if let gmailUrl = gmailUrl, UIApplication.shared.canOpenURL(gmailUrl) { return gmailUrl }
else if let outlookUrl = outlookUrl, UIApplication.shared.canOpenURL(outlookUrl) { return outlookUrl }
else if let yahooMail = yahooMail, UIApplication.shared.canOpenURL(yahooMail){ return yahooMail }
else if let sparkUrl = sparkUrl, UIApplication.shared.canOpenURL(sparkUrl) { return sparkUrl }
return nil
}
Is there a way to simply ask, what is your default mail app, and open it to write the email?
Using the method above it only checks for apple mail app as default and whatever I have defined for the 3rd parties.