发送电子邮件 - MFMailComposeResult

10

我正试图将我的一个应用程序从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)
    }
}

输入图像描述

3个回答

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

哇!真的很好用...我不知道为什么之前没看到过。非常感谢!!! - Miguel Ferri
现在您应该使用.rawValue而不是.value。 - jaminguy
在 Swift 2.2 版本中可以正常工作,不确定是哪个版本修复了这个问题。 - Oleksii Nezhyborets

3

已测试并完全可用 在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)
}

0

我觉得在Swift 3中,所谓的100%测试只完成了大约50%。当我尝试编译时,编译器并不真正喜欢它。不过,XCode帮助我修复了问题,现在已经能够正常工作了(截至2017年9月1日)。下面是最终编译的代码:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接