单击警报按钮以显示MFMailComposeViewController

编程入门 行业动态 更新时间:2024-10-25 14:29:06
本文介绍了单击警报按钮以显示MFMailComposeViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在单击警报按钮时尝试使用MFMailComposeViewController.但是,当我单击警报按钮时,控制器不会弹出.代码如下.我使用了扩展程序,并在单击警报按钮时尝试调用sendmail函数.

Trying to use MFMailComposeViewController when click alert button. But when I click alert button, controller doesn't pop-up. The code is below. I used extension and I'm trying to call sendmail function when clicking alert button.

extension Alert:MFMailComposeViewControllerDelegate { func sendmail(){ let mailComposeViewController = configureMailController() if MFMailComposeViewController.canSendMail() { let VC = storyboard?.instantiateViewController(withIdentifier: "MainVC") VC?.present(mailComposeViewController, animated: true, completion: nil) } else { showMailError() } } func configureMailController() -> MFMailComposeViewController { let mailComposerVC = MFMailComposeViewController() let VC = storyboard?.instantiateViewController(withIdentifier: "MainVC") mailComposerVC.mailComposeDelegate = VC as? MFMailComposeViewControllerDelegate mailComposerVC.setToRecipients(["**"]) mailComposerVC.setSubject("**") mailComposerVC.setMessageBody("\n\n\n\nModel: \nSistem versiyon: )\nuygulamaversiyon:", isHTML: false) return mailComposerVC } func showMailError() { let sendMailErrorAlert = UIAlertController(title: "Could not send email", message: "Your device could not send email", preferredStyle: .alert) let dismiss = UIAlertAction(title: "Ok", style: .default, handler: nil) sendMailErrorAlert.addAction(dismiss) let VC = storyboard?.instantiateViewController(withIdentifier: "MainVC") VC?.present(sendMailErrorAlert, animated: true, completion: nil) } public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { controller.dismiss(animated: true, completion: nil) } }

推荐答案

您的代码尝试在您使用instantiateViewController创建的新视图控制器上呈现邮件视图控制器(以及错误警报).由于此新VC本身不是您应用程序VC层次结构的一部分,因此它和邮件VC都不会出现在屏幕上.

Your code tries to present the mail view controller (and also the error alert) on a new view controller which you create with instantiateViewController. Since this new VC itself is not part of your app's VC hierarchy, neither it nor the mail VC will appear on screen.

要实现此目的,您可以将 是应用程序一部分的视图控制器传递到您的sendmail方法中:

To make this work, you can either pass a view controller which is part of your app into your sendmail method:

func sendmail(_ root: UIViewController) { ... root.present(mailComposeViewController, animated: true, completion: nil) ...

或者您可以使用可全局访问的应用程序VC;在使用根VC的简单应用中应该可以工作:

Or you can use a globally accessible VC of your app; in a simple app using the root VC should work:

func sendmail() { ... let root = UIApplication.shared.keyWindow?.rootViewController root?.present(mailComposeViewController, animated: true, completion: nil) ...

我建议采用第一种方法,因为它更灵活(例如,即使您的VC层次结构变得更加复杂,它也允许您在任何地方打开邮件屏幕).

I'd suggest the first approach because it is more flexible (e.g. it allows you to open your mail screen anywhere, even when your VC hierarchy gets more complex).

更多推荐

单击警报按钮以显示MFMailComposeViewController

本文发布于:2023-11-27 15:52:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638639.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   单击   按钮   MFMailComposeViewController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!