如何使用 Swift(iOS)在邮件中发送附件?
了解如何在电子邮件中发送附件非常重要,因为大多数应用程序都具有共享功能。因此,拥有实践经验很重要。
在这篇文章中,我们将了解如何使用 Swift 在邮件中发送附件。
那么,让我们开始吧。
为此,我们将使用 MFMailComposeViewController,这是一个标准的视图控制器,其界面允许用户管理、编辑和发送电子邮件。
您可以在此处了解更多信息 https://developer.apple.com/documentation/messageui/mfmailcomposeviewcontroller
我们还将使用 MFMailComposeViewControllerDelegate 来处理 MFMailComposeResult 的结果。
您可以在此处了解更多信息 https://developer.apple.com/documentation/messageui/mfmailcomposeviewcontrollerdelegate
我们将创建一个示例应用程序来理解:
步骤 1 - 打开 Xcode → 单视图应用程序 → 将其命名为 EmailAttachment
步骤 2 - 打开 Main.storyboard 并添加一个按钮,将其命名为发送邮件,如下所示:

步骤 3 - 创建 @IBAction 并将其命名为 btnSendMail,如下所示:
@IBAction func btnSendMail(_ sender: Any) { }步骤 4 - 在 ViewController.swift 中,导入 MessageUI
import MessageUI
步骤 5 - 将类确认到 MFMailComposeViewControllerDelegate
class ViewController: UIViewController, MFMailComposeViewControllerDelegate
步骤 6 - 将附件文件添加到项目中:

步骤 7 - 在 btnSendMail 中编写以下函数:
@IBAction func btnSendMail(_ sender: Any) {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.setToRecipients(["test@gmail.com"])
mail.setSubject("GREETING")
mail.setMessageBody("Welcome to Tutorials Point!", isHTML: true)
mail.mailComposeDelegate = self
//add attachment
if let filePath = Bundle.main.path(forResource: "sampleData", ofType: "json") {
if let data = NSData(contentsOfFile: filePath) {
mail.addAttachmentData(data as Data, mimeType: "application/json" , fileName: "sampleData.json")
}
}
present(mail, animated: true)
}
else {
print("Email cannot be sent")
}
}搞定!
但是我们也需要处理其他条件,例如邮件已发送、已取消或失败。为此,我们才确认了上述协议。
让我们实现委托方法:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
if let _ = error {
self.dismiss(animated: true, completion: nil)
}
switch result {
case .cancelled:
print("Cancelled")
break
case .sent:
print("Mail sent successfully")
break
case .failed:
print("Sending mail failed")
break
default:
break
}
controller.dismiss(animated: true, completion: nil)
}搞定!
在真机上运行程序:

完整代码
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
}
@IBAction func btnSendMail(_ sender: Any) {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.setToRecipients(["test@gmail.com"])
mail.setSubject("GREETING")
mail.setMessageBody("Welcome to Tutorials Point!", isHTML: true)
mail.mailComposeDelegate = self
if let filePath = Bundle.main.path(forResource: "sampleData", ofType: "json") {
if let data = NSData(contentsOfFile: filePath) {
mail.addAttachmentData(data as Data, mimeType: "application/json" , fileName: "sampleData.json")
}
}
present(mail, animated: true)
}
else {
print("Email cannot be sent")
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
if let _ = error {
self.dismiss(animated: true, completion: nil)
}
switch result {
case .cancelled:
print("Cancelled")
break
case .sent:
print("Mail sent successfully")
break
case .failed:
print("Sending mail failed")
break
default:
break
}
controller.dismiss(animated: true, completion: nil)
}
}
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP