Chức năng soạn mail trong ứng dụng iOS

Giới thiệu Với mục đích đưa các chức năng email vào ứng dụng iOS, MFMailComposeViewController là một View Controller cho phép cung cấp những tính năng email cơ bản nhất như soạn và gửi thư. Ngoài ra nó còn cho phép người dùng thiết lập các giá trị mặc định như danh sách người gửi,

Giới thiệu

Với mục đích đưa các chức năng email vào ứng dụng iOS, MFMailComposeViewController là một View Controller cho phép cung cấp những tính năng email cơ bản nhất như soạn và gửi thư.
Ngoài ra nó còn cho phép người dùng thiết lập các giá trị mặc định như danh sách người gửi, tiêu đề, nội dung thư.
Bài viết này sẽ từng bước tích hợp View Controller này vào ứng dụng iOS của bạn.

Khởi tạo.

MFMailComposeViewController là một thành phần trong framework MessageUI nên ta cần phải khao báo thư viện này để sử dụng.
import MessageUI

Bước tiếp theo, ta cần phải kiểm tra xem ứng dụng có quyền gửi email hay không, bằng cách thêm đoạn chương trình dưới đây

func composeEmail() {
    guard MFMailComposeViewController.canSendMail() else {
        let alert = UIAlertController(title: "Send email", message: "This device cannot send emails.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
 
        self.present(alert, animated: true, completion: nil)
        return       
    }
}

Phương thức canSendMail sẽ kiểm tra xem ứng dụng có thể gửi mail không, nếu không sẽ hiển thị thông báo lỗi.

Cấu hình đối tượng MFMailComposeViewController

Trước tiên ta cần khởi tạo View Controller MFMailComposeViewController

let email = MFMailComposeViewController()
email.mailComposeDelegate = self

Ngoài ra ta có thể khởi tạo các giá trị sau trước khi hiển thị giao diện soạn mail

  • Tiêu đề của mail

email.setSubject("New post on my site!")

  • Danh sách người nhận
    email.setToRecipients([“[email protected]“, “[email protected]“, “[email protected]“])
    Ngoài ra ta có thể sử dụng các chức năng CC và BCC để thay đổi danh sách người nhận thông qua 2 phương thức setCcRecipients()setBccRecipients()

-Thông tin người gửi

email.setPreferredSendingEmailAddress("[email protected]")

  • Nội dụng mail dưới dạng HTML hoặc là dạng Text

email.setMessageBody("This is a sample message!", isHTML: false)

Toàn bộ đoạn chương trình cho các bước trên:

func composeEmail() {
    guard MFMailComposeViewController.canSendMail() else {
        let alert = UIAlertController(title: "Send email", message: "This device cannot send emails.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
        return        
    }
    
    let email = MFMailComposeViewController()
    email.mailComposeDelegate = self
    
    // Set email subject.
    email.setSubject("New post on my site!")
    
    // Set recipients.
    email.setToRecipients(["[email protected]", "[email protected]", "[email protected]"])
 
    // Set the From field email address.
    email.setPreferredSendingEmailAddress("[email protected]")
    
    // Set email body.
    email.setMessageBody("This is a sample text!", isHTML: false)
    
    // Present the email compose view controller.
    self.present(email, animated: true, completion: nil)
}

Thực thi phương thức của mail delegate

MFMailComposeViewController cung cấp phương thức cho phép theo dõi lỗi và trạng thái của mail thông qua 2 tham số:

  • error: để kiểm soát lỗi
  • result; để theo dõi trạng thái của mail
extension UIViewController: MFMailComposeViewControllerDelegate {
    public func mailComposeController(_ controller: MFMailComposeViewController,
                                      didFinishWith result: MFMailComposeResult,
                                      error: Error?) {        
        guard error == nil else {
            print(error!.localizedDescription)
            controller.dismiss(animated: true, completion: nil)
            return
        }
        
        switch result {
            case .sent: print("The email was sent")
            case .saved: print("The email was saved")
            case .cancelled: print("The email was cancelled")
            case .failed: print("Failed to send email")
            @unknown default: break
        }
        
        controller.dismiss(animated: true, completion: nil)
    }
}

Nguồn tham khảo

https://serialcoder.dev/text-tutorials/ios-tutorials/email-composer-on-ios/

Nguồn: viblo.asia

Bài viết liên quan

9 Mẹo lập trình Web “ẩn mình” giúp tiết kiệm hàng giờ đồng hồ

Hầu hết các lập trình viên (kể cả những người giỏi) đều tốn thời gian x

Can GPT-4o Generate Images? All You Need to Know about GPT-4o-image

OpenAI‘s GPT-4o, introduced on March 25, 2025, has revolutionized the way we create visual con

Khi nào nên dùng main, section, article, header, footer, và aside trong HTML5

HTML5 đã giới thiệu các thẻ ngữ nghĩa giúp cấu trúc nội dung web một cách có

So sánh Webhook và API: Khi nào nên sử dụng?

Trong lĩnh vực công nghệ thông tin và phát triển phần mềm, Webhook và API là hai th