How can I launch an email client on ios using Swfit

前端 未结 4 613
误落风尘
误落风尘 2021-01-02 06:04

In Android, I can launch an email client from my android application using its Intent mechanism. In ios, how can I launch its email client from my ios application using Swif

相关标签:
4条回答
  • 2021-01-02 06:24
    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.

    0 讨论(0)
  • 2021-01-02 06:26

    Update iOS 10+

    //Use for Open mail application or open any link with browser!

    let url = NSURL(string: "mailto:your_mail_here@mail.com")
    
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
       // Fallback on earlier versions
       UIApplication.shared.openURL(url)
    }
    
    0 讨论(0)
  • 2021-01-02 06:30

    I found a great solution from Hacking in Swift by Paul Hudson. In Swift 3, Add import MessageUI to the top of the file and make the class conform to MFMailComposeViewControllerDelegate protocol.

    func sendEmail() {
      if MFMailComposeViewController.canSendMail() {
       let mail = MFMailComposeViewController()
       mail.mailComposeDelegate = self
       mail.setToRecipients(["example@example.com"])
       mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)
    
       present(mail, animated: true)
       } else {
       // show failure alert
      }
    }
    
    // MARK: MFMailComposeViewControllerDelegate Conformance
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
    }
    
    0 讨论(0)
  • 2021-01-02 06:38

    SWIFT 3: The openEmail function will try to use the iOS Mail app if it is available (user has atleast one email account setup). Otherwise it will use the mailto: url (see full example at the bottom of my reply) to launch a mail client.

    import MessageUI
    // Make your view controller conform to MFMailComposeViewControllerDelegate
    class Foo: UIViewController, MFMailComposeViewControllerDelegate {
    
        func openEmail(_ emailAddress: String) {
            // If user has not setup any email account in the iOS Mail app
            if !MFMailComposeViewController.canSendMail() {
                print("Mail services are not available")
                let url = URL(string: "mailto:" + emailAddress)
                UIApplication.shared.openURL(url!)
                return
            }
    
            // Use the iOS Mail app
            let composeVC = MFMailComposeViewController()
            composeVC.mailComposeDelegate = self
            composeVC.setToRecipients([emailAddress])
            composeVC.setSubject("")
            composeVC.setMessageBody("", isHTML: false)
    
            // Present the view controller modally.
            self.present(composeVC, animated: true, completion: nil)
        }
    
        // MARK: MailComposeViewControllerDelegate
        func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
            // Dismiss the mail compose view controller.
            controller.dismiss(animated: true, completion: nil)
        }
    }
    

    An all out mailto example with subject, body, and cc:

    "mailto:me@gmail.com?subject=Hey whats up man!&body=This is from a great example I found online.&cc=someoneelse@yahooo.com&bcc=whostillusesthis@hotmail.com"

    0 讨论(0)
提交回复
热议问题