mailto links now open on iOS

fixes #2036

Extends `URL` with an email address `var` for `mailto` schemes and adds a decisionHandler for `mailto` schemes on `WebViewController`. If the device cannot send mail, an alert is displayed.
This commit is contained in:
Stuart Breckenridge
2020-05-03 21:33:57 +08:00
parent d09541a0c0
commit f901436211
3 changed files with 50 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ import RSCore
import Account
import Articles
import SafariServices
import MessageUI
protocol WebViewControllerDelegate: class {
func webViewController(_: WebViewController, articleExtractorButtonStateDidUpdate: ArticleExtractorButtonState)
@@ -310,6 +311,23 @@ extension WebViewController: WKNavigationDelegate {
let vc = SFSafariViewController(url: url)
self.present(vc, animated: true)
}
} else if components?.scheme == "mailto" {
decisionHandler(.cancel)
guard let emailAddress = components?.url?.emailAddress else {
return
}
if MFMailComposeViewController.canSendMail() {
let mailComposeViewController = MFMailComposeViewController()
mailComposeViewController.setToRecipients([emailAddress])
mailComposeViewController.mailComposeDelegate = self
self.present(mailComposeViewController, animated: true, completion: {})
} else {
let alert = UIAlertController(title: "Error", message: "This device cannot send emails.", preferredStyle: .alert)
alert.addAction(.init(title: "Dismiss", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
} else {
decisionHandler(.allow)
}
@@ -666,3 +684,10 @@ private extension WebViewController {
}
}
// MARK: MFMailComposeViewControllerDelegate
extension WebViewController: MFMailComposeViewControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
self.dismiss(animated: true, completion: nil)
}
}