[英]Sending an email from swift 3
我正在嘗試使用發送電子郵件選項設置應用。
我有這個代碼:
import Foundation
import MessageUI
import UIKit
class emailClass: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if !MFMailComposeViewController.canSendMail() {
print("Mail services are not available")
return
}
sendEmail()
}
func sendEmail() {
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["address@example.com"])
composeVC.setSubject("Hello!")
composeVC.setMessageBody("Hello this is my message body!", isHTML: false)
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
// Check the result or perform other tasks.
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
所以我收到此消息:“郵件服務不可用”。 現在我已經登錄了iCloud中的模擬器設備...所以我認為應該這樣做但不是。 為什么這不起作用? 你能告訴我什么是錯的,我怎么能繼續前進?
它不適用於模擬器。 請在iPhone設備上測試。 您可以參考Apple Developer Portal - MFMailComposeViewController
這就是我做到的。 看起來你很好地遵循了文檔,我想我會添加我的變體,以防它幫助其他人。 另外,這是對當前(2017年8月)語法的更新。
符合MFMailComposeViewControllerDelegate協議,並檢查設備是否可以發送郵件。
import Foundation
import UIKit
import MessageUI
class WelcomeViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if !MFMailComposeViewController.canSendMail() {
print("Mail services are not available")
return
}
}
我的應用程序使用IBAction啟動郵件組合。
@IBAction func sendFeedbackButtonTapped(_ sender: Any) {
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["exampleEmail@email.com"])
composeVC.setSubject("Message Subject")
composeVC.setMessageBody("Message content.", isHTML: false)
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)
}
關於以下mailComposeController函數,文檔說
郵件撰寫視圖控制器不會自動解除。 當用戶點擊按鈕發送電子郵件或取消界面時,郵件撰寫視圖控制器調用其委托的mailComposeController(_:didFinishWith:error :)方法。 您對該方法的實現必須顯式地關閉視圖控制器,如清單3所示。您還可以使用此方法檢查操作的結果。
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult, error: Error?) {
// Check the result or perform other tasks.
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
}
}
如果應用程序在真實設備中運行,代碼似乎很好並且工作正常
MFMailComposeViewController.canSendMail() // returns false for simulators.
你不能在模擬器上測試它,你將能夠測試基本的東西,如UI,如何在取消/發送按鈕點擊發生的事情。
要進行測試,您必須使用設備,設備中的Mail應用程序應配置一些郵件(例如:abc@xyz.com)。
希望有所幫助。
在Swift 5中輕松發送電子郵件,您需要確認並實施MFMailComposeViewControllerDelegate並檢查我們是否可以在此設備上發送電子郵件
這是我用於完成任務的一小段代碼
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: IBAction Method for Button click
@IBAction func sendEmail(_ sender: Any) {
//TODO: You should chack if we can send email or not
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["you@yoursite.com"])
mail.setSubject("Email Subject Here")
mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)
present(mail, animated: true)
} else {
print("Application is not able to send an email")
}
}
//MARK: MFMail Compose ViewController Delegate method
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}
}
PS:請不要忘記你需要在真實設備上進行測試
您的代碼的問題是,
//以模態方式顯示視圖控制器。 self.present(composeVC,animated:true,completion:nil)
出現在自己;-)
如果你不知道當前的視圖控制器,只需在RootViewController上顯示它,即
UIApplication.shared.keyWindow?.rootViewController?.present(...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.