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
Xcode 8 Build Crash on iOS 9.2 and Below
Using Nsuserdefaults with Xcode 8 and iOS 10
iOS 6 Facebook Posting Procedure Ends Up with "Remote_App_Id Does Not Match Stored Id"
Xcode Canvas for Swiftui Previews Does Not Show Up
How to Determine Height of Uicollectionview with Flowlayout
Updating iOS Badge Without Push Notifications
How to Get Rgb Values from Uicolor
Itms-90535 Unable to Publish iOS App with Latest Google Signin Sdk
iOS 7.0 No Code Signing Identities Found
How to Make a Button Continually Call a Function When Held Down (Spritekit)
Setting Tableheaderview Height Dynamically
Dtassetproviderservice Could Not Start Dtxconnection with Simulator
iPhone Core Data "Automatic Lightweight Migration"