簡體   English   中英

如何使用Swfit在ios上啟動電子郵件客戶端

[英]How can I launch an email client on ios using Swfit

在Android中,我可以使用其Intent機制從我的Android應用程序啟動電子郵件客戶端。 在ios中,如何使用Swift從我的ios應用程序啟動其電子郵件客戶端?

謝謝。

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

請注意,這僅適用於設備,而不適用於模擬器。

我從Paul Hudson的Hacking in Swift中找到了一個很好的解決方案。 在Swift 3中,將import MessageUI添加到文件的頂部,並使該類符合MFMailComposeViewControllerDelegate協議。

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)
}

更新iOS 10+

//用於打開郵件應用程序或打開瀏覽器的任何鏈接!

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)
}

SWIFT 3: openEmail功能將嘗試使用iOS Mail應用程序(如果可用)(用戶至少有一個電子郵件帳戶設置)。 否則,它將使用mailto:url(請參閱我的回復底部的完整示例)來啟動郵件客戶端。

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)
    }
}

一個包含subject,body和cc的全部mailto示例:

“mailto:me@gmail.com?subject =嘿嘿man!&body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body body

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM