ios 系统调用邮箱
iOS 系统调用邮箱的方式有两种:
- 使用 Mailto URL Scheme:可以通过调用 Mailto URL Scheme 来打开系统默认的邮件应用,创建新的邮件,并填充收件人、主题、正文等信息。示例代码如下:
let email = "example@example.com"
let subject = "Hello"
let body = "This is the email body"
if let url = URL(string: "mailto:\(email)?subject=\(subject)&body=\(body)") {
UIApplication.shared.open(url)
}
- 使用 MFMailComposeViewController 类:可以通过实例化 MFMailComposeViewController 类来创建一个邮件视图控制器,可以自定义收件人、主题、正文等信息,并通过 present 方法来展示邮件视图控制器。示例代码如下:
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
func sendEmail() {
guard MFMailComposeViewController.canSendMail() else {
// 邮件功能不可用
return
}
let email = "example@example.com"
let subject = "Hello"
let body = "This is the email body"
let mailComposeVC = MFMailComposeViewController()
mailComposeVC.mailComposeDelegate = self
mailComposeVC.setToRecipients([email])
mailComposeVC.setSubject(subject)
mailComposeVC.setMessageBody(body, isHTML: false)
present(mailComposeVC, animated: true)
}
// 邮件发送结果回调
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}
}
需要注意的是,在使用 MFMailComposeViewController 类之前,需要在项目的 Target 中添加 MessageUI.framework,并在代码中引入 MessageUI 模块。
原文地址: https://www.cveoy.top/t/topic/jevD 著作权归作者所有。请勿转载和采集!