mfmailcomposeviewcontrollerdelegate objective-c uiviewcontroller swift ios8 mfmailcomposer

objective c - mfmailcomposeviewcontrollerdelegate - MFMailComposeViewController en Swift



swift 3 send email (5)

Este es un código de muestra:

import UIKit import MessageUI class ViewController: UIViewController, MFMailComposeViewControllerDelegate { @IBAction func showEmail(sender : AnyObject) { var emailTitle = "Test Email" var messageBody = "This is a test email body" var toRecipents = ["[email protected]"] var mc: MFMailComposeViewController = MFMailComposeViewController() mc.mailComposeDelegate = self mc.setSubject(emailTitle) mc.setMessageBody(messageBody, isHTML: false) mc.setToRecipients(toRecipents) self.presentViewController(mc, animated: true, completion: nil) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) { switch result { case MFMailComposeResultCancelled: NSLog("Mail cancelled") case MFMailComposeResultSaved: NSLog("Mail saved") case MFMailComposeResultSent: NSLog("Mail sent") case MFMailComposeResultFailed: NSLog("Mail sent failure: %@", [error.localizedDescription]) default: break } self.dismissViewControllerAnimated(false, completion: nil) } }

En la función mailComposeController me aparece un error en cada expresión de caso :

No se pudo encontrar una sobrecarga ''~ ='' que acepte los argumentos proporcionados.

¿Qué estoy haciendo mal?



En swift 3, puedes usar este código claro:

@IBAction func sendMail(_ sender: Any) { print(MFMailComposeViewController.canSendMail()) if MFMailComposeViewController.canSendMail() { let mail = MFMailComposeViewController() mail.mailComposeDelegate = self mail.setToRecipients(["[email protected]"]) mail.setMessageBody("<p>This is test Mail!</p>", isHTML: true) present(mail, animated: true) } else { let email = "[email protected]" if let url = URL(string: "mailto:/(email)") { UIApplication.shared.open(url) } } } func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { controller.dismiss(animated: true) switch result { case .cancelled: print("Mail cancelled") case .saved: print("Mail saved") case .sent: self.allertInfo(_title: "Mail Info", _message: "Mail is sent successfuly", _actionTitle: "OK") print("Mail sent") case .failed: self.allertInfo(_title: "Mail Info", _message: "Mail isn''t sent.", _actionTitle: "OK") print("Mail sent failure: /(error?.localizedDescription)") default: break } } func allertInfo(_title:String, _message:String, _actionTitle:String) { let alert = UIAlertController(title: _title, message: _message, preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: _actionTitle, style: UIAlertActionStyle.default, handler: nil)) self.present(alert, animated: true, completion: nil) }


En swift 3.0 -> Sintaxis de cambio

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { switch result.rawValue { case MFMailComposeResult.Cancelled.rawValue: print("Mail cancelled") case MFMailComposeResult.Saved.rawValue: print("Mail saved") case MFMailComposeResult.Sent.rawValue: print("Mail sent") case MFMailComposeResult.Failed.rawValue: print("Mail sent failure: %@", [error!.localizedDescription]) default: break } // Dismiss the mail compose view controller. controller.dismissViewControllerAnimated(true, completion: nil) }


en Swift 2.0 haz esto:

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 } controller.dismissViewControllerAnimated(true, completion: nil) }


MFMailComposeResult documentación de MFMailComposeResult en Xcode 5 y Xcode 6. En Swift, MFMailComposeResult es una estructura

struct MFMailComposeResult { init(_ value: CUnsignedInt) // available in iPhone 3.0 var value: CUnsignedInt }

con MFMailComposeResultCancelled como una constante de tipo MFMailComposeResult :

var MFMailComposeResultCancelled: MFMailComposeResult { get }

Si bien es una enumeración en Objective-C:

enum MFMailComposeResult { MFMailComposeResultCancelled, MFMailComposeResultSaved, MFMailComposeResultSent, MFMailComposeResultFailed }; typedef enum MFMailComposeResult MFMailComposeResult; // available in iPhone 3.0

Para que su código funcione, tendrá que comparar sus valores que son CUnsignedInt .

Así que tendrás que escribir el siguiente código:

func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) { switch result.value { case MFMailComposeResultCancelled.value: println("Mail cancelled") case MFMailComposeResultSaved.value: println("Mail saved") case MFMailComposeResultSent.value: println("Mail sent") case MFMailComposeResultFailed.value: println("Mail sent failure: /(error.localizedDescription)") default: break } self.dismissViewControllerAnimated(false, completion: nil) }