【问题标题】:Cannot dismiss MFMailComposeViewController that I called from SKScene无法关闭我从 SKScene 调用的 MFMailComposeViewController
【发布时间】:2019-08-17 21:58:30
【问题描述】:

我正在尝试从我的游戏应用程序中发送电子邮件。在我的一个 SKScene 中,当您按下它时,我有一个精灵,它调用 FeedbackVC().sendEmail()。这会打开电子邮件 viewController,但它不会正确关闭。这是我的整个 FeedbackVC 课程。我使用了函数 getTopMostViewController 因为没有它我收到错误“警告:尝试呈现其视图不在窗口层次结构中!”。我的代码将成功打开带有预填充字段的 MFMailComposeViewController,如果我按下发送按钮,它实际上会将电子邮件发送到我的电子邮件,但它不会关闭,如果我尝试取消电子邮件它也不会关闭。为什么我的 viewController 不会关闭,以便在发送或取消电子邮件后继续返回我的游戏?

import Foundation
import MessageUI

class FeedbackVC: UINavigationController, MFMailComposeViewControllerDelegate {

    func getTopMostViewController() -> UIViewController? {
        var topMostViewController = UIApplication.shared.keyWindow?.rootViewController
        while let presentedViewController = topMostViewController?.presentedViewController {
            topMostViewController = presentedViewController
        }
        return topMostViewController
    }

    func sendEmail() {
        if MFMailComposeViewController.canSendMail() {
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self

            mail.setToRecipients(["support@supportemail.com"])
            mail.setSubject("In-App Feedback")
            mail.setMessageBody("", isHTML: false)
            self.getTopMostViewController()!.present(mail, animated: true, completion: nil)
        } else {
            print("Failed To Send Email!")
        }
    }

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true, completion: nil)
    }
}

我还尝试在 sendEmail() 函数中设置 UINavigationControllerDelegate。

mail.delegate = self as? UINavigationControllerDelegate

我也尝试过弹出视图控制器并返回到 mailComposeController 中最顶层的视图控制器。

popToRootViewContoller(animated: true)

getTopMostViewController()?.dismiss(animated: true, completion: nil)

我尝试按照https://developer.apple.com/documentation/messageui/mfmailcomposeviewcontroller 上的指南进行操作,但它不起作用,因为我认为我的情况有所不同,因为我要从 SKScene 转到 MFMailCompose ViewController,然后再返回 SKScene。

【问题讨论】:

  • 您需要在呈现视图控制器上调用dismiss - 即您从 getTopMostViewController 检索到的视图控制器 - 将该对象保存在属性中,以便您可以从委托方法 developer.apple.com/documentation/uikit/uiviewcontroller/… 中使用它
  • 我尝试使用“var presentingVC = UIViewController()”在类级别初始化它然后在我的 func getTopMostViewController 我添加了“presentingVC = topMostViewController!”然后在我有趣的 mailComposeController 中添加了“presentingVC.dismiss(animated: true, completion: nil)”,但它仍然没有关闭 MFMailComposeViewController。

标签: ios swift viewcontroller mfmailcomposeviewcontroller skscene


【解决方案1】:

我是参与此项目的其他开发人员之一。发帖以防有人遇到类似问题。

我们试图以如下所示的方式调用我们的 FeedbackVC:

if nodeTapped.name == "Feedback" {
  let vc = FeedbackVC()
  vc.emailButtonTapped(foo)
}

这将创建 FeedbackVC 类,调用 emailButtonTapped 方法,然后在退出 if 语句时从内存中释放该类。这意味着单击取消或发送将尝试访问已释放的空间,从而导致 EXC_BAD_ACCESS 错误。我通过将 vc 声明为类变量而不是在 if 语句中声明它来解决此问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多