OGeek|极客世界-中国程序员成长平台

标题: ios - MFMailComposeViewController 不会关闭 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:25
标题: ios - MFMailComposeViewController 不会关闭

我的应用中有以下代码用于发送电子邮件:

let ToRecipents = ["recipient here"]
        let subject = "subject here"
        let MessageBody = "message here"

        let mc : MFMailComposeViewController = MFMailComposeViewController()

        mc.mailComposeDelegate = self


        mc.setToRecipients(ToRecipents)
        mc.setSubject(subject)
        mc.setMessageBody(MessageBody, isHTML: false)

        self.present(mc, animated: true, completion: nil)

    }

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {

        controller.dismiss(animated: true, completion: nil)

    }

我似乎无法让 MFMailComposeViewController 关闭,我做错了什么??



Best Answer-推荐答案


一般不调用委托(delegate)方法时:
• 检查是否设置了代理。你正确地使用 mc.mailComposeDelegate = self
• 检查您设置的委托(delegate)方法是否符合协议(protocol)。
• 检查委托(delegate)方法是否在您作为委托(delegate)传递的对象中正确实现(这就是您的问题所在)。阅读文档,或删除方法声明,让编译器/IDE/XCode 为您自动完成它。在各种教程之间的 Swift 中,它们经常出现问题,因为 Swift 1、Swift 2、Swift 3/4 已重命名方法并导致问题,教程仅关注 Swift 版本。

你的方法:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?)

documentation 中的方法:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)

由于它们不一样,iOS SDK 应该检查委托(delegate)是否响应正确的方法,那么你的不应该被调用,因为它不匹配。

关于ios - MFMailComposeViewController 不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48168807/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4