From ae1c97562741ba7cf595474c31f92414984b4c90 Mon Sep 17 00:00:00 2001 From: Yannik Bloscheck Date: Tue, 24 Jun 2025 16:16:01 +0200 Subject: [PATCH] [ios] Improved handling of non-default mail clients Signed-off-by: Yannik Bloscheck --- .../Maps/UI/MailComposer/MailComposer.swift | 65 +++---------------- 1 file changed, 10 insertions(+), 55 deletions(-) diff --git a/iphone/Maps/UI/MailComposer/MailComposer.swift b/iphone/Maps/UI/MailComposer/MailComposer.swift index 77c4c3e4c..69e69c693 100644 --- a/iphone/Maps/UI/MailComposer/MailComposer.swift +++ b/iphone/Maps/UI/MailComposer/MailComposer.swift @@ -42,65 +42,20 @@ final class MailComposer: NSObject { } private static func sendEmailWith(subject: String, body: String, toRecipients recipients: [String], attachmentFileURL: URL? = nil) { - // If the attachment file path is provided, the default mail composer should be used. - if let attachmentFileURL { - if MWMMailViewController.canSendMail(), let attachmentData = try? Data(contentsOf: attachmentFileURL) { - let mailViewController = MWMMailViewController() - mailViewController.mailComposeDelegate = mailComposer - mailViewController.setSubject(subject) - mailViewController.setMessageBody(body, isHTML:false) - mailViewController.setToRecipients(recipients) - mailViewController.addAttachmentData(attachmentData, mimeType: "application/zip", fileName: attachmentFileURL.lastPathComponent) - topViewController.present(mailViewController, animated: true, completion:nil) - } else { - showMailComposingAlert(recipients: recipients) - } - return - } - - // Before iOS 14, try to open alternate email apps first, assuming that if users installed them, they're using them. - let os = ProcessInfo().operatingSystemVersion - if (os.majorVersion < 14 && (openGmail(subject: subject, body: body, recipients: recipients) || - openOutlook(subject: subject, body: body, recipients: recipients))) { - return - } - - // From iOS 14, it is possible to change the default mail app, and mailto should open a default mail app. - if !openDefaultMailApp(subject: subject, body: body, recipients: recipients) { + // If the attachment file path is provided, the default mail composer should be used, if possible. + if let attachmentFileURL, MWMMailViewController.canSendMail(), let attachmentData = try? Data(contentsOf: attachmentFileURL) { + let mailViewController = MWMMailViewController() + mailViewController.mailComposeDelegate = mailComposer + mailViewController.setSubject(subject) + mailViewController.setMessageBody(body, isHTML:false) + mailViewController.setToRecipients(recipients) + mailViewController.addAttachmentData(attachmentData, mimeType: "application/zip", fileName: attachmentFileURL.lastPathComponent) + topViewController.present(mailViewController, animated: true, completion:nil) + } else if !openDefaultMailApp(subject: subject, body: body, recipients: recipients) { showMailComposingAlert(recipients: recipients) } } - private static func openOutlook(subject: String, body: String, recipients: [String]) -> Bool { - var components = URLComponents(string: "ms-outlook://compose")! - components.queryItems = [ - URLQueryItem(name: "to", value: recipients.joined(separator: ";")), - URLQueryItem(name: "subject", value: subject), - URLQueryItem(name: "body", value: body), - ] - - if let url = components.url, UIApplication.shared.canOpenURL(url) { - UIApplication.shared.open(url) - return true - } - return false - } - - private static func openGmail(subject: String, body: String, recipients: [String]) -> Bool { - var components = URLComponents(string: "googlegmail://co")! - components.queryItems = [ - URLQueryItem(name: "to", value: recipients.joined(separator: ";")), - URLQueryItem(name: "subject", value: subject), - URLQueryItem(name: "body", value: body), - ] - - if let url = components.url, UIApplication.shared.canOpenURL(url) { - UIApplication.shared.open(url) - return true - } - return false - } - private static func openDefaultMailApp(subject: String, body: String, recipients: [String]) -> Bool { var components = URLComponents(string: "mailto:\(recipients.joined(separator: ";"))") components?.queryItems = [