2016-04-25 180 views
9

我想從我的應用程序發送郵件。我正在做SWIFT的第一步,並且我堅持了一個觀點。我想按下按鈕並打開郵件。你能告訴我如何做按鈕連接?我認爲它應該是一個動作,但我不知道在哪裏把它的代碼如何從我的iOS應用程序發送郵件 - SWIFT

import UIKit 
import MessageUI 

class ViewController: UIViewController, MFMailComposeViewControllerDelegate { 

    func sendEmail() { 
     let mailVC = MFMailComposeViewController() 
     mailVC.mailComposeDelegate = self 
     mailVC.setToRecipients([]) 
     mailVC.setSubject("Subject for email") 
     mailVC.setMessageBody("Email message string", isHTML: false) 

     presentViewController(mailVC, animated: true, completion: nil) 
    } 

    // MARK: - Email Delegate 

    func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { 
     controller.dismissViewControllerAnimated(true, completion: nil) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

    } 
} 

回答

10

變化sendEmail這樣的:

@IBAction func sendEmail(sender: AnyObject) { 
    let mailVC = MFMailComposeViewController() 
    mailVC.mailComposeDelegate = self 
    mailVC.setToRecipients([]) 
    mailVC.setSubject("Subject for email") 
    mailVC.setMessageBody("Email message string", isHTML: false) 

    presentViewController(mailVC, animated: true, completion: nil) 
} 

,並連接在Interface Builder您的按鈕,這個動作

+0

我一定要刪除FUNC sndEmail()? – jonathan

+0

@jonathan只是改變FUNC sendEmail(){線 – gvuksic

+0

抱歉愚蠢的問題,但是當它就是這樣..它依然犯規工作 – jonathan

5

要發送郵件,一般使用MFMailComposer。它可以在設備上進行測試,因爲它不適用於iOS模擬器。

爲了測試郵件服務是否可用,使用下面的功能,

if !MFMailComposeViewController.canSendMail() { 
    print("Mail services are not available") 
    return 
} 

,並在你的函數或按鈕的動作發送郵件,使用下面的代碼。

let composeVC = MFMailComposeViewController() 
composeVC.mailComposeDelegate = self 

// Configure the fields of the interface. 
composeVC.setToRecipients(["[email protected]"]) 
composeVC.setSubject("Hello World!") 
composeVC.setMessageBody("Hello from iOS!", isHTML: false) 

// Present the view controller modally. 
self.presentViewController(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.dismissViewControllerAnimated(true, completion: nil) 
} 
+0

感謝您的答案。並且如何啓用郵件服務? –

1

安東普拉東諾夫補充:進口MessageUI在源文件的乞討,當你聲明類爲視圖控制器添加協議:類FirstVC:UIViewController中,MFMailComposeViewControllerDelegate {

5

斯威夫特3

let composer = MFMailComposeViewController() 

if MFMailComposeViewController.canSendMail() { 
    composer.mailComposeDelegate = self 
    composer.setToRecipients(["Email1", "Email2"]) 
    composer.setSubject("Test Mail") 
    composer.setMessageBody("Text Body", isHTML: false) 
    present(composer, animated: true, completion: nil) 
} 

委託方法

class SendMailViewController: MFMailComposeViewControllerDelegate { 
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 
     dismiss(animated: true, completion: nil) 
    } 
} 
1

導入庫第一:

import MessageUI 

組委託,如:

MFMailComposeViewControllerDelegate 

寫漂亮的代碼:

@IBAction func buttonHandlerSendEmail(_ sender: Any) { 

    let mailComposeViewController = configureMailComposer() 
    if MFMailComposeViewController.canSendMail(){ 
     self.present(mailComposeViewController, animated: true, completion: nil) 
    }else{ 
     print("Can't send email") 
    } 
} 
func configureMailComposer() -> MFMailComposeViewController{ 
    let mailComposeVC = MFMailComposeViewController() 
    mailComposeVC.mailComposeDelegate = self 
    mailComposeVC.setToRecipients([self.textFieldTo.text!]) 
    mailComposeVC.setSubject(self.textFieldSubject.text!) 
    mailComposeVC.setMessageBody(self.textViewBody.text!, isHTML: false) 
    return mailComposeVC 
} 

還寫這樣的委託方法:

//MARK: - MFMail compose method 
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 
    controller.dismiss(animated: true, completion: nil) 
} 

enter image description here

100%的工作和測試

相關問題