单击警报按钮以显示MFMailComposeViewController [英] Click alert button to show 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!