How to Open Mail App from Swift

How to open the default mail app on iOS 14 without a compose view?

I ended up half-solving it by asking the user with an alert view about their preference, because I did not find a way to query iOS about it directly.

So first am showing an alert view like this:

func askUserForTheirPreference(in presentingViewController: UIViewController) {
let alertController = UIAlertController(title: nil, message: "pleaseConfirmWithApp", preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "Apple Mail", style: .default) { action in
self.open(presentingViewController, .applemail)
})
alertController.addAction(UIAlertAction(title: "Google Mail", style: .default) { action in
self.open(presentingViewController, .googlemail)
})
alertController.addAction(UIAlertAction(title: "Microsoft Outlook", style: .default) { action in
self.open(presentingViewController, .outlook)
})
alertController.addAction(UIAlertAction(title: "Protonmail", style: .default) { action in
self.open(presentingViewController, .protonmail)
})
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel) { action in
os_log("Cancelling", log: Self.log, type: .debug)
})
presentingViewController.present(alertController, animated: true)
}

Then, I am responding to the user's choice like this:

func open(_ presentingViewController: UIViewController, _ appType: AppType) {
switch appType {
case .applemail: UIApplication.shared.open(URL(string: "message:")!, completionHandler: { handleAppOpenCompletion(presentingViewController, $0) })
case .googlemail: UIApplication.shared.open(URL(string: "googlegmail:")!, completionHandler: { handleAppOpenCompletion(presentingViewController, $0) })
case .outlook: UIApplication.shared.open(URL(string: "ms-outlook:")!, completionHandler: { handleAppOpenCompletion(presentingViewController, $0) })
case .protonmail: UIApplication.shared.open(URL(string: "protonmail:")!, completionHandler: { handleAppOpenCompletion(presentingViewController, $0) })
}
}

private func handleAppOpenCompletion(_ presentingViewController: UIViewController, _ isSuccess: Bool) {
guard isSuccess else {
let alertController = UIAlertController(title: nil, message: "thisAppIsNotInstalled", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: .cancel))
presentingViewController.present(alertController, animated: true)
return
}
}

enum AppType {
case applemail, googlemail, outlook, protonmail
}

A clear limitation of this approach is of course that I am limiting the user to very specific apps (in this case Google Mail, iOS "default" Mail, Microsoft Outlook and ProtonMail).
So this approach does not really scale well.

But at least, you can cover a few favorite ones and go from there based on your users' feedback.

The main reason for jumping through these hoops of asking the first is that, at least at the moment, it seems impossible to get that information from iOS directly.
I also could not find a URL scheme that would always open the chosen default Mail app without showing the compose new email view.

iOS Swift: Open Mail app in inbox emails

I resolved using the following code:

   let mailURL = URL(string: "message://")!
if UIApplication.shared.canOpenURL(mailURL) {
UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
}

How to launch the iOS mail app in Swift?

Not tested myself but maybe this answer will help you:

Apparently Mail supports a second url scheme message:// which (I suppose) allows you to open a specific message if it was fetched by your application. If you do not provide a full message url, it will just open Mail:

let mailURL = URL(string: "message://")!
if UIApplication.shared.canOpenURL(mailURL) {
UIApplication.shared.openURL(mailURL)
}

Taken from: Launch Apple Mail App from within my own App?

iOS 14 - How to open Default Mail App programmatically?

Apparently you have to add mailto to the LSApplicationQueriesSchemes to make it work.

Open mail app with a specific mail in the inbox with Swift

Found a quite easy Solution:

To open the Mail App with a given Message-ID you can simply open it via the message: URL Scheme.

No Need for AppleScript ;-).

Button(action: {
NSWorkspace.shared.open(URL(string: "message:%3C...YOUR-MESSAGE_ID....%3E")!)
}) {
Text("Open E-Mail in Mail App")
}

Open mail app without creating email draft

This is not possible to do in a purely cross-platform manner without the mailto:// URI which all systems understand. If you wanted to just open the mail client, you would have to check if your target OS supports such intent / URI and open it in a platform-specific manner.

Update: I have found platform specific solutions for Android and iOS.

Android

var intent = PackageManager.GetLaunchIntentForPackage("com.android.email");
StartActivity(intent);

iOS

UIApplication.SharedApplication.OpenUrl("message://");

UWP

In case of UWP mailto: seems to be the right option according to Docs. Unfortunately from my testing it does try to create a new e-mail with the built in Outlook Mail app. I will report that as a issue.

How can I launch an email client on ios using Swfit


let url = NSURL(string: "mailto:jon.doe@mail.com")
UIApplication.sharedApplication().openURL(url)

Note that this works only on a device, not in the simulator.



Related Topics



Leave a reply



Submit