发送电子邮件 - MFMailComposeResult

时间:2014-06-09 17:29:23

标签: switch-statement swift

我正在尝试将我的一个应用程序从Obj-C迁移到Swift,我遇到了电子邮件管理方面的问题。
我按小时搜索但是我没有找到解决这个问题的方法 基本上,我正在尝试迁移func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!)函数。

问题是交换机内没有任何选项有效。

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

3 个答案:

答案 0 :(得分:20)

请勿忘记,您还可以使用.rawValue(旧版Swift中的.value)对您将变量结果与之比较的特定结果类型进行比较:

    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")
    }

答案 1 :(得分:3)

经过测试并100%正常工作 在swift 3.0中,这已经改变了,现在你应该做这样的事情:

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)
}

答案 2 :(得分:0)

在我看来,在swift 3中测试的100%仅测试了约50%。当我尝试它时,编译器并不喜欢它。 XCode帮助我修复它到9-1-17工作的那个。下面的代码是最终编译的代码:

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)
}