3

Auf dem gleichen Viewcontroller können wir eine E-Mail oder eine Textnachricht senden, um Informationen an einen Freund zu senden. Die Textnachricht in App funktioniert vollständig. Aber für die E-Mail öffnet sich die E-Mail-App in meiner App mit allen Informationen, die ich schreiben wollte, aber es ist unmöglich, sie durch Drücken von Abbrechen abzuweisen, nichts passiert. Ich habe versucht, mc.mailComposeDelegate = self oder mc.delegate = self und MFMailComposeViewControllerDelegate ist auch an der Spitze. Ich schaute alles im Internet, ich habe keine Erklärung gefunden. mailComposeController wird nie aufgerufen! Haben Sie eine Idee?SWIFT - mailComposeDelegate nicht aufgerufen: Abbrechen von MFMailComposeViewController funktioniert nicht

class inviteAFriendViewController: UIViewController, MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate { 

@IBAction func emailButtonDidTouch(sender: AnyObject) { 
    sendEmail() 
} 

func sendEmail() { 
    let emailTitle = "text" 
    let messageBody = "text" 
    let toRecipents = [""] 

    let mc = MFMailComposeViewController() 

    //mc.mailComposeDelegate = self 

    mc.delegate = self 

    mc.setSubject(emailTitle) 
    mc.setMessageBody(messageBody, isHTML: false) 
    mc.setToRecipients(toRecipents) 

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

func mailComposeController(controller2: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { 
    switch result.rawValue { 
    case MFMailComposeResultCancelled.rawValue: 
     print("Mail cancelled") 
     controller2.dismissViewControllerAnimated(true, completion: nil) 
    case MFMailComposeResultSaved.rawValue: 
     print("Mail saved") 
     controller2.dismissViewControllerAnimated(true, completion: nil) 
    case MFMailComposeResultSent.rawValue: 
     print("Mail sent") 
     controller2.dismissViewControllerAnimated(true, completion: nil) 
    case MFMailComposeResultFailed.rawValue: 
     print("Mail sent failure.") 
     controller2.dismissViewControllerAnimated(true, completion: nil) 
    default: 
     break 
    } 
    controller2.dismissViewControllerAnimated(true, completion: nil) 
} 

Antwort

13

Ich habe es ohne Probleme funktioniert, aber meine Delegatmethode sieht ein wenig anders zu verkaufen:

func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError?) { 
    switch result.rawValue { 
    case MFMailComposeResultCancelled.rawValue: 
     print("Mail cancelled") 
    case MFMailComposeResultSaved.rawValue: 
     print("Mail saved") 
    case MFMailComposeResultSent.rawValue: 
     print("Mail sent") 
    case MFMailComposeResultFailed.rawValue: 
     print("Mail sent failure: %@", [error.localizedDescription]) 
    default: 
     break 
    } 
    self.dismissViewControllerAnimated(true, completion: nil) 
} 

Sie versuchen diese kann.

Und Sie brauchen mc.mailComposeDelegate = self und nicht die mc.delegate = self

+0

Oh mein Gott, es funktioniert, ich weiß nicht, wer du bist, aber danke 1000 mal! Einen schönen Tag noch! – Oscar

2

Zunächst einmal setzen verwenden

mc.mailComposeDelegate = self 

statt

mc.delegate = self 

Außerdem bei Swift 3 Delegate-Methode ist irgendwie upd ated das ist:

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