簡體   English   中英

如何通過UI Label(Swift 2 IOS 9)在應用程序中發送電子郵件?

[英]How to send email in app from UI Label (Swift 2 IOS 9)?

通過操作本教程http://www.raywenderlich.com/113772/uisearchcontroller-tutorial

我有一個顯示人員的表格視圖,單擊該單元格時,用戶將被重定向到另一個顯示其圖片和電子郵件的視圖。 我希望能夠讓用戶單擊電子郵件地址並通過電子郵件發送給他們 我研究並找到了類似的教程https://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift/

上面的教程的問題是,當測試運行新代碼時,ios模擬器彈出一個錯誤,並且不會顯示撰寫的電子郵件(也許是小故障?),如果模擬器未給出錯誤,我將不知道如何顯示多封電子郵件用戶選擇的對象。 對這個問題的解決方案或任何替代方案的任何幫助將非常感謝!

當您嘗試打開郵件時,模擬器將崩潰。 而是在實際設備上嘗試。

要撰寫郵件,請執行以下操作將它們添加到類MFMessageComposeViewControllerDelegateMFMailComposeViewControllerDelegate

didSelectRowAtIndexPath ,這是您在tableView中按下一行時調用的函數。 請執行下列操作:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    let candy = candies[indexPath.row]
    var mail: MFMailComposeViewController!

    // yourArray is the array that you use to populate the tableView
    // .mail is the variable in the object (I´m assuming you´re using objects in your array)
    let toRecipients = [candy[indexPath.row].email]
    let subject = "Feedback"
    let body = "<br><br><p>I have a \(UIDevice.currentDevice().modelName).<br> And iOS version \(UIDevice.currentDevice().systemVersion).<br</p>"

    mail = MFMailComposeViewController()
    mail.mailComposeDelegate = self
    mail.setToRecipients(toRecipients)
    mail.setSubject(subject)
    mail.setMessageBody(body, isHTML: true)

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

如果需要使用下面的委托方法

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

    func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
        switch (result.rawValue) {
        case MessageComposeResultCancelled.rawValue:
            self.dismissViewControllerAnimated(true, completion: nil)
        case MessageComposeResultFailed.rawValue:
            self.dismissViewControllerAnimated(true, completion: nil)
        case MessageComposeResultSent.rawValue:
            self.dismissViewControllerAnimated(true, completion: nil)
        default:
            break;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM