From c59e3e4ddfcc92b0462961da93568f290a50deb7 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Fri, 8 Jan 2021 12:15:16 +0800 Subject: [PATCH 1/3] Emails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Acquiesce to the user’s request for a different default mail app. --- .../iOS/Article/WebViewController.swift | 20 +++++------------- Multiplatform/iOS/Info.plist | 4 ++++ iOS/Article/WebViewController.swift | 21 ++++++------------- iOS/Resources/Info.plist | 4 ++++ 4 files changed, 19 insertions(+), 30 deletions(-) diff --git a/Multiplatform/iOS/Article/WebViewController.swift b/Multiplatform/iOS/Article/WebViewController.swift index 151084f75..8a552f39a 100644 --- a/Multiplatform/iOS/Article/WebViewController.swift +++ b/Multiplatform/iOS/Article/WebViewController.swift @@ -315,15 +315,14 @@ extension WebViewController: WKNavigationDelegate { } else if components?.scheme == "mailto" { decisionHandler(.cancel) - guard let emailAddress = url.emailAddress else { + guard let _ = url.emailAddress else { return } - if MFMailComposeViewController.canSendMail() { - let mailComposeViewController = MFMailComposeViewController() - mailComposeViewController.setToRecipients([emailAddress]) - mailComposeViewController.mailComposeDelegate = self - self.present(mailComposeViewController, animated: true, completion: {}) + if UIApplication.shared.canOpenURL(url) { + UIApplication.shared.open(url, options: [.universalLinksOnly : false]) { (success) in + print(success) + } } else { let alert = UIAlertController(title: NSLocalizedString("Error", comment: "Error"), message: NSLocalizedString("This device cannot send emails.", comment: "This device cannot send emails."), preferredStyle: .alert) alert.addAction(.init(title: NSLocalizedString("Dismiss", comment: "Dismiss"), style: .cancel, handler: nil)) @@ -418,15 +417,6 @@ extension WebViewController: UIScrollViewDelegate { } -// MARK: MFMailComposeViewControllerDelegate -extension WebViewController: MFMailComposeViewControllerDelegate { - - func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { - self.dismiss(animated: true, completion: nil) - } - -} - // MARK: JSON private struct ImageClickMessage: Codable { diff --git a/Multiplatform/iOS/Info.plist b/Multiplatform/iOS/Info.plist index d797bc4a1..cab22bf05 100644 --- a/Multiplatform/iOS/Info.plist +++ b/Multiplatform/iOS/Info.plist @@ -81,5 +81,9 @@ group.$(ORGANIZATION_IDENTIFIER).NetNewsWire.iOS AppIdentifierPrefix $(AppIdentifierPrefix) + LSApplicationQueriesSchemes + + mailto + diff --git a/iOS/Article/WebViewController.swift b/iOS/Article/WebViewController.swift index 40d910ba8..35b6d28c3 100644 --- a/iOS/Article/WebViewController.swift +++ b/iOS/Article/WebViewController.swift @@ -356,16 +356,14 @@ extension WebViewController: WKNavigationDelegate { } else if components?.scheme == "mailto" { decisionHandler(.cancel) - guard let emailAddress = url.emailAddress else { + guard let _ = url.emailAddress else { return } - if MFMailComposeViewController.canSendMail() { - let mailComposeViewController = MFMailComposeViewController() - mailComposeViewController.setToRecipients([emailAddress]) - mailComposeViewController.setSubject(url.valueFor("subject") ?? "") - mailComposeViewController.mailComposeDelegate = self - self.present(mailComposeViewController, animated: true, completion: {}) + if UIApplication.shared.canOpenURL(url) { + UIApplication.shared.open(url, options: [.universalLinksOnly : false]) { (success) in + print(success) + } } else { let alert = UIAlertController(title: NSLocalizedString("Error", comment: "Error"), message: NSLocalizedString("This device cannot send emails.", comment: "This device cannot send emails."), preferredStyle: .alert) alert.addAction(.init(title: NSLocalizedString("Dismiss", comment: "Dismiss"), style: .cancel, handler: nil)) @@ -459,14 +457,7 @@ extension WebViewController: UIScrollViewDelegate { } -// MARK: MFMailComposeViewControllerDelegate -extension WebViewController: MFMailComposeViewControllerDelegate { - - func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { - self.dismiss(animated: true, completion: nil) - } - -} + // MARK: JSON diff --git a/iOS/Resources/Info.plist b/iOS/Resources/Info.plist index 094a9981c..e843a3e9e 100644 --- a/iOS/Resources/Info.plist +++ b/iOS/Resources/Info.plist @@ -54,6 +54,10 @@ CFBundleVersion $(CURRENT_PROJECT_VERSION) + LSApplicationQueriesSchemes + + mailto + LSRequiresIPhoneOS NSAppTransportSecurity From ea77504f3aad8e8787e776f27ea78483bde89a6c Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Sat, 9 Jan 2021 05:56:13 +0800 Subject: [PATCH 2/3] percent encodes email addresses --- Multiplatform/iOS/Article/WebViewController.swift | 8 +++----- iOS/Article/WebViewController.swift | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/Multiplatform/iOS/Article/WebViewController.swift b/Multiplatform/iOS/Article/WebViewController.swift index 8a552f39a..cd5ecf160 100644 --- a/Multiplatform/iOS/Article/WebViewController.swift +++ b/Multiplatform/iOS/Article/WebViewController.swift @@ -315,14 +315,12 @@ extension WebViewController: WKNavigationDelegate { } else if components?.scheme == "mailto" { decisionHandler(.cancel) - guard let _ = url.emailAddress else { + guard let emailAddress = URL(string: (url.emailAddress?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed))!) else { return } - if UIApplication.shared.canOpenURL(url) { - UIApplication.shared.open(url, options: [.universalLinksOnly : false]) { (success) in - print(success) - } + if UIApplication.shared.canOpenURL(emailAddress) { + UIApplication.shared.open(emailAddress, options: [.universalLinksOnly : false], completionHandler: nil) } else { let alert = UIAlertController(title: NSLocalizedString("Error", comment: "Error"), message: NSLocalizedString("This device cannot send emails.", comment: "This device cannot send emails."), preferredStyle: .alert) alert.addAction(.init(title: NSLocalizedString("Dismiss", comment: "Dismiss"), style: .cancel, handler: nil)) diff --git a/iOS/Article/WebViewController.swift b/iOS/Article/WebViewController.swift index 35b6d28c3..fbe56fff9 100644 --- a/iOS/Article/WebViewController.swift +++ b/iOS/Article/WebViewController.swift @@ -356,14 +356,12 @@ extension WebViewController: WKNavigationDelegate { } else if components?.scheme == "mailto" { decisionHandler(.cancel) - guard let _ = url.emailAddress else { + guard let emailAddress = URL(string: (url.emailAddress?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed))!) else { return } - if UIApplication.shared.canOpenURL(url) { - UIApplication.shared.open(url, options: [.universalLinksOnly : false]) { (success) in - print(success) - } + if UIApplication.shared.canOpenURL(emailAddress) { + UIApplication.shared.open(emailAddress, options: [.universalLinksOnly : false], completionHandler: nil) } else { let alert = UIAlertController(title: NSLocalizedString("Error", comment: "Error"), message: NSLocalizedString("This device cannot send emails.", comment: "This device cannot send emails."), preferredStyle: .alert) alert.addAction(.init(title: NSLocalizedString("Dismiss", comment: "Dismiss"), style: .cancel, handler: nil)) From f85c1234a503114e18d9638cd5e4e5dde30966bb Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Sat, 9 Jan 2021 06:09:50 +0800 Subject: [PATCH 3/3] Adds to the URL-Extensions file --- Multiplatform/iOS/Article/WebViewController.swift | 2 +- Shared/Extensions/URL-Extensions.swift | 5 +++++ iOS/Article/WebViewController.swift | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Multiplatform/iOS/Article/WebViewController.swift b/Multiplatform/iOS/Article/WebViewController.swift index cd5ecf160..18d8c44c4 100644 --- a/Multiplatform/iOS/Article/WebViewController.swift +++ b/Multiplatform/iOS/Article/WebViewController.swift @@ -315,7 +315,7 @@ extension WebViewController: WKNavigationDelegate { } else if components?.scheme == "mailto" { decisionHandler(.cancel) - guard let emailAddress = URL(string: (url.emailAddress?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed))!) else { + guard let emailAddress = url.percentEncodedEmailAddress else { return } diff --git a/Shared/Extensions/URL-Extensions.swift b/Shared/Extensions/URL-Extensions.swift index bbe70a1b1..707877cca 100644 --- a/Shared/Extensions/URL-Extensions.swift +++ b/Shared/Extensions/URL-Extensions.swift @@ -15,6 +15,11 @@ extension URL { scheme == "mailto" ? URLComponents(url: self, resolvingAgainstBaseURL: false)?.path : nil } + /// Percent encoded `mailto` URL for use with `canOpenUrl`. If the URL doesn't contain the `mailto` scheme, this is `nil`. + var percentEncodedEmailAddress: URL? { + scheme == "mailto" ? self.string.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)?.url : nil + } + /// URL pointing to current app version release notes. static var releaseNotes: URL { let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "" diff --git a/iOS/Article/WebViewController.swift b/iOS/Article/WebViewController.swift index fbe56fff9..6024d02dd 100644 --- a/iOS/Article/WebViewController.swift +++ b/iOS/Article/WebViewController.swift @@ -356,7 +356,7 @@ extension WebViewController: WKNavigationDelegate { } else if components?.scheme == "mailto" { decisionHandler(.cancel) - guard let emailAddress = URL(string: (url.emailAddress?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed))!) else { + guard let emailAddress = url.percentEncodedEmailAddress else { return }