Reputation: 103
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)
}
}
Upvotes: 10
Views: 3785
Reputation: 500
It seems to me that the 100% tested in swift 3 was only about 50% tested. When I tried it the compiler did not really like it. XCode help me fix it though to one that did work as of 9-1-17. The code that follows is the code that did finally compile:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?){
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.dismiss(animated: true, completion: nil)
}
Upvotes: 0
Reputation: 1245
Tested and works 100% In swift 3.0 this is changed and now you should make something like this:
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)
}
Upvotes: 3
Reputation: 32104
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")
}
Upvotes: 20