1. 程式人生 > >[Xcode10 實際操作]八、網絡與多線程-(7)使用MessageUI框架,創建並發送一封帶有附件的郵件

[Xcode10 實際操作]八、網絡與多線程-(7)使用MessageUI框架,創建並發送一封帶有附件的郵件

override type pie loading 顯示 進制數 anim int message

本文將演示如何使用MessageUI框架,創建並發送一封帶有附件的郵件。

使用郵件編輯視圖控制器(MFMailComposeViewController)實現郵件功能。

在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】

  1 import UIKit
  2 //導入需要使用的框架MessageUI
  3 //該框架為發送短信和郵件,提供了具有標準組成接口的、自定義的視圖控制器
  4 import MessageUI
  5 
  6 //添加郵件編輯視圖控制器代理協議MFMailComposeViewControllerDelegate,
  7
//委托方法包含在該代理協議中。 8 class ViewController: UIViewController, MFMailComposeViewControllerDelegate { 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 // Do any additional setup after loading the view, typically from a nib. 13 14 //創建一個位置在(40,80),尺寸為(240,44)的顯示區域
15 let rect = CGRect(x: 40, y: 80, width: 240, height: 44) 16 //初始化一個按鈕控件,並設置其位置和尺寸信息, 17 //當點擊按鈕時,談出郵件編輯窗口 18 let button = UIButton(frame: rect) 19 //設置按鈕在正常狀態下的標題文字 20 button.setTitle("Write a mail", for: UIControl.State()) 21 //設置按鈕的背景顏色為紫色
22 button.backgroundColor = UIColor.purple 23 //給按鈕綁定點擊事件 24 button.addTarget(self, action: #selector(ViewController.sendEmail), 25 for: UIControl.Event.touchUpInside) 26 //將按鈕添加到當前視圖控制器的根視圖 27 self.view.addSubview(button) 28 } 29 30 //創建一個方法,用來響應按鈕的點擊事件 31 @objc func sendEmail() 32 { 33 if(!MFMailComposeViewController.canSendMail()) 34 { 35 print("提示用戶需要在[設置 > 郵件]頁面添加郵箱賬號。") 36 return 37 } 38 //初始化一個郵件視圖控制器 39 let mailBox = MFMailComposeViewController() 40 //創建一個數組,用來設置收件人的郵箱 41 let recipients = ["[email protected]"] 42 //設置郵件發送到的目標郵箱 43 mailBox.setToRecipients(recipients) 44 //設置郵件的標題文字 45 mailBox.setSubject("Information!") 46 47 //創建一個字符串,作為郵件的主題內容 48 let message = "<font color=‘blue‘>Hi, can you do me a favor</font>" 49 //郵件的內容支持網頁代碼格式 50 mailBox.setMessageBody(message, isHTML: true) 51 52 //從項目中加載一張圖片,作為郵件的附件 53 let attachedPic = UIImage(named: "Pic1.png") 54 //將圖片的內容,壓縮並轉化為二進制數據格式 55 let imageData = attachedPic!.pngData() 56 //將轉換格式後的數據對象,作為郵件的附件 57 mailBox.addAttachmentData(imageData!, mimeType: "", fileName: "Pic.png") 58 59 //設置郵件發送的監聽代理對象,為當前的視圖控制器對象 60 mailBox.mailComposeDelegate = self 61 //顯示郵件的編輯視圖 62 self.present(mailBox, animated: true, completion: nil) 63 } 64 65 //添加一個方法,用來響應郵件發送的結果事件 66 func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) 67 { 68 //遍歷郵件的幾種發送狀態 69 70 //當用戶取消郵件發送時 71 if(result == MFMailComposeResult.cancelled) 72 { 73 print(">>>> MFMailComposeResultCancelled") 74 } 75 //當郵件發送失敗時 76 else if(result == MFMailComposeResult.failed) 77 { 78 //在控制臺打印輸出郵件發送失敗的日誌 79 print(">>>> MFMailComposeResultFailed") 80 } 81 //當郵件被成功保存時 82 else if(result == MFMailComposeResult.saved) 83 { 84 //在控制臺打印輸出郵件被成功保存的日誌 85 print(">>>> MFMailComposeResultSaved") 86 } 87 //當郵件發送成功時 88 else if(result == MFMailComposeResult.sent) 89 { 90 //在控制臺打印輸出郵件發送成功的日誌 91 print(">>>> MFMailComposeResultSent") 92 } 93 //發送郵件後,關閉郵件視圖控制器 94 controller.dismiss(animated: true, completion: nil) 95 } 96 97 override func didReceiveMemoryWarning() { 98 super.didReceiveMemoryWarning() 99 // Dispose of any resources that can be recreated. 100 } 101 }

[Xcode10 實際操作]八、網絡與多線程-(7)使用MessageUI框架,創建並發送一封帶有附件的郵件