I just want to open MS Outlook app and see mailto screen using URL Scheme at iOS
ms-outlook://compose?to=joe@example.com&subject=Hello
How do I get mailto links in iOS (safari) to open the Outlook app?
Instead of 'mailto:' protocol you can use below protocol to open outlook app
ms-outlook://compose?to=joe@example.com&subject=Hello
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.
Launch Apple Mail App from within my own App?
Apparently Mail application supports 2nd url scheme - message://
which ( I suppose) allows to open specific message if it was fetched by the application. If you do not provide message url it will just open mail application:
NSURL* mailURL = [NSURL URLWithString:@"message://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
[[UIApplication sharedApplication] openURL:mailURL];
}
Open eMail from App with predefined text in iOS
You can do it using MFMailComposeViewController
:
import MessageUI
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["email@email.com"])
mailComposerVC.setSubject("Subject")
mailComposerVC.setMessageBody("Body", isHTML: false)
self.presentViewController(mailComposerVC, animated: true, completion: nil)
Also, you need to implement mailComposeController:didFinishWithResult:error:
from MFMailComposeViewControllerDelegate
where you should dismiss MFMailComposeViewController
Related Topics
How to Print Out a Property's Contents Using Xcode Debugger
Avplayer with Playback Controls of Avplayerviewcontroller
How to Access User Defined Runtime Attribute from the 'Sender' Object
Confirm Back Button on Uinavigationcontroller
Check If 3D Touch Is Supported and Enabled on the iOS9 Device
Bypass Code Signing with Xcode 6
iOS - Spritekit - How to Calculate the Distance Between Two Nodes
Installing Openssl Library for Xcode
Mkmapview Doesn't Zoom Correctly While User Tracking Mode Is Mkusertrackingmodefollowwithheading
Insert String at Cursor Position of Uitextfield
Code Signing Issue in Xcode Version 8
How to Create Multi Line Uisegmentedcontrol
How to Capture Taps Ignored by Wkwebview
How We Can Set the Light Content Style of Status Bar in iOS 9 for Whole Application