Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending emails - MFMailComposeResult

I'm trying to migrate one of my applications from Obj-C to Swift and I've a problem with the emails management.
I searched by hours but I did not found how solve this problem.
Basically, I'm trying to migrate the func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) function.

The problem is that no options, inside the switch, are valid.

func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!)
{
    switch result.value
    {
        case CUnsignedInt(MFMailComposeResultCancelled):
            var alert = UIAlertController(
                title: NSLocalizedString("sendingStatus", tableName: "LocalizationFile", comment:"sendingStatus"),
                message: NSLocalizedString("emailCancelledByUser", tableName: "LocalizationFile", comment:"emailCancelledByUser"),
                preferredStyle: UIAlertControllerStyle.Alert)
            self.presentViewController(alert, animated: true, completion: nil)
        case MFMailComposeResult(MFMailComposeResultFailed):
            var alert = UIAlertController(
                title: NSLocalizedString("sendingStatus", tableName: "LocalizationFile", comment:"sendingStatus"),
                message: NSLocalizedString("emailSentFailed", tableName: "LocalizationFile", comment:"emailSentFailed"),
                preferredStyle: UIAlertControllerStyle.Alert)
            self.presentViewController(alert, animated: true, completion: nil)
        case MFMailComposeResultSaved:
            var alert = UIAlertController(
                title: NSLocalizedString("sendingStatus", tableName: "LocalizationFile", comment:"sendingStatus"),
                message: NSLocalizedString("emailSaved", tableName: "LocalizationFile", comment:"emailSaved"),
                preferredStyle: UIAlertControllerStyle.Alert)
            self.presentViewController(alert, animated: true, completion: nil)
        default:
            var alert = UIAlertController(
                title: NSLocalizedString("sendingStatus", tableName: "LocalizationFile", comment:"sendingStatus"),
                message: NSLocalizedString("emailNotSent", tableName: "LocalizationFile", comment:"emailNotSent"),
                preferredStyle: UIAlertControllerStyle.Alert)
            self.presentViewController(alert, animated: true, completion: nil)
    }
}

enter image description here

like image 557
Miguel Ferri Avatar asked Jun 09 '14 17:06

Miguel Ferri


1 Answers

Don't forget that you can also use .rawValue (.value in older versions of Swift) on the specific result types you're comparing your variable result to:

    var result:MFMailComposeResult = MFMailComposeResultCancelled

    switch(result.value) { // <-- Here, note .value is being used
        case MFMailComposeResultCancelled.value: // <-- And here as well!
            print("Cancelled")
        default:
            print("Default")
    }
like image 135
Craig Otis Avatar answered Oct 20 '22 20:10

Craig Otis