1. 程式人生 > >[Xcode10 實際操作]八、網路與多執行緒-(5)使用UIApplication物件傳送郵件

[Xcode10 實際操作]八、網路與多執行緒-(5)使用UIApplication物件傳送郵件

本文將演示如何使用應用程式單例物件,傳送郵件的功能。

在專案導航區,開啟檢視控制器的程式碼檔案【ViewController.swift】

注:需要使用真機進行測試。

 1 import UIKit
 2 
 3 class ViewController: UIViewController {
 4 
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7         // Do any additional setup after loading the view, typically from a nib.
8 9 //建立一個字串,儲存一個電子郵箱, 10 //並留意它的字首格式【mailto:】 11 let tel = "mailto:[email protected]" 12 //然後將字串轉換為網址物件 13 let url = URL(string:tel) 14 //獲取應用程式單例物件, 15 //使用它開啟網頁的功能, 16 //調出郵件編寫面板 17 UIApplication.shared.openURL(url!)
18 } 19 20 override func didReceiveMemoryWarning() { 21 super.didReceiveMemoryWarning() 22 // Dispose of any resources that can be recreated. 23 } 24 }