14

I'm creating an iOS application with a password reset feature that sends an email to the user. After sending the email I want to display a UIAlertController to the user asking them if they would like to open the mail application.

I've seen various posts on here along the lines of:

let url = NSURL(string: "mailto:")
UIApplication.sharedApplication().openURL(url!)

This works but unfortunately it starts a new message which is not what I want. I only want to launch the application so the user can see their inbox.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
Kyle Decot
  • 20,715
  • 39
  • 142
  • 263

5 Answers5

25

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?

dehlen
  • 7,325
  • 4
  • 43
  • 71
  • unverschaemt, given that this is a Swift-question, would you be bothered if I edited your answer into Swift? – nhgrif Apr 02 '15 at 22:12
  • does one have to get permission to do this? For example if I wanted to open the gmail app from my app for the sake of verifying a users email, would I need to ask google for permission to open their app from mine? – yaboi Jun 24 '15 at 01:58
  • to validate a users email you'd better choose a regex and also send a confirmation email if you want to. In any case you can use the url-scheme from third party apps to open them with your content. That's why they made them in the first place. However you can't control like the whole app you have a predefined set of functions you can use from the third party app. – dehlen Jun 24 '15 at 12:30
3

The Swift 3.0.1 way of just opening the Mail app goes as follows:

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

As "dehlen" correctly stated, using the message:// scheme will only open the mail app, if no further information is provided.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
2

Obviously a few years later...I had to add a completion handler for Xcode 10.2.1 swift 5.

This works perfectly-

    let emailURL = NSURL(string: "message://")!

    if UIApplication.shared.canOpenURL(emailURL as URL)
    {
        UIApplication.shared.open(emailURL as URL, options: [:],completionHandler: nil)
    }
Superlative
  • 514
  • 1
  • 4
  • 14
0

Since UIApplication.shared.openURL() method has been deprecated and we can use URL() directly in place of NSURL(), the updated version of this question's answer is:

let mailURL = URL(string: "message://")!
if UIApplication.shared.canOpenURL(mailURL) {
    UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
}
Sagun Raj Lage
  • 2,326
  • 2
  • 18
  • 28
-1

This will work with Xcode 11.5:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath.row {     
    case 5:
        openEmailApp(email: "email@gmail.com")
        
    default:
        break
    }
}

func openEmailApp(email: String) {
    if let url = URL(string: "mailto: \(email)") {
        UIApplication.shared.open(url)
Pang
  • 9,564
  • 146
  • 81
  • 122