2017-01-09 3 views
1

Ich versuche, MessageUI und MFMailComposeViewController zu implementieren, um E-Mails in meiner App zu senden. Alles scheint gut zu funktionieren, außer dass ich den Mail-Client nicht wirklich verlassen kann, wenn ich den Prozess gestartet habe. Wenn ich send drücke, sendet es und tut dann nichts mehr (kehrt nicht zur App zurück). Wenn ich Abbrechen klicke, werden Entwurf und Kit nicht gelöscht (oder zurück zur App).In neue Nachricht E-Mail-Client in iOS-App stecken

import UIKit 
import MessageUI 

class ContactFormViewController: UITableViewController, MFMailComposeViewControllerDelegate { 

... 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    switch indexPath.item{ 
     case 0: 
      followOnEmail() 
     default: 
      break 
    } 
} 

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

func followOnEmail(){ 
    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("") 
    composeVC.setMessageBody("Please enter your message below.", isHTML: false) 

    if MFMailComposeViewController.canSendMail(){ 
     // Present the view controller modally. 
     self.present(composeVC, animated: true, completion: nil) 
    } 
    else{ 
     self.showSendMailErrorAlert() 
    } 
} 

... 

func showSendMailErrorAlert(){ 
    let alertController = UIAlertController(title: "Could Not Send Email", message: "Your device could not send email. Please check e-mail configuration and try again.", preferredStyle: .alert) 
    let defaultAction = UIAlertAction(title: "Close", style: .default, handler: nil) 
    alertController.addAction(defaultAction) 
    present(alertController, animated: true, completion: nil) 
} 

}

Antwort

1

Swift 3 geändert, um die leicht API.

ändern
private func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { 

zu

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 
Verwandte Themen