MFMailComposeViewController、Swift 4、Xcode 9
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48703157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MFMailComposeViewController, Swift 4, Xcode 9
提问by ?or?e Nilovi?
I have a problem with MFMailComposeViewControllerDelegate function.
我对 MFMailComposeViewControllerDelegate 函数有问题。
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
The warning says
警告说
Instance method 'mailComposeController(:didFinishWith:error:)' nearly matches optional requirement 'mailComposeController(:didFinishWith:error:)' of protocol 'MFMailComposeViewControllerDelegate'
Make 'mailComposeController(_:didFinishWith:error:)' private to silence this warning
实例方法 'mailComposeController( :didFinishWith:error:)' 几乎匹配协议 'MFMailComposeViewControllerDelegate' 的可选要求 'mailComposeController(:didFinishWith:error:)'
将 'mailComposeController(_:didFinishWith:error:)' 设为私有以消除此警告
I need to return the user to the App and dismiss MFMailComposeViewController after clicking cancel and this function is not triggered.
单击取消后,我需要将用户返回到应用程序并关闭 MFMailComposeViewController 并且不会触发此功能。
Yes, I added the delegate composeVC.mailComposeDelegate = self
是的,我添加了委托 composeVC.mailComposeDelegate = self
If someone had a similar problem, I would appreciate the help. Thanks
如果有人有类似的问题,我将不胜感激。谢谢
EDIT
编辑
This behavior is happening only when I set the language to Swift 4. I just went back few commits and it's working perfectly fine with Swift 3.2
只有当我将语言设置为 Swift 4 时才会发生这种行为。我只返回了几次提交,并且它在 Swift 3.2 中运行良好
Basically, this is the code:
基本上,这是代码:
class TechSupportVC: UIViewController, MFMailComposeViewControllerDelegate {
let composeVC = MFMailComposeViewController()
override func viewDidLoad() {
super.viewDidLoad()
composeVC.mailComposeDelegate = self
composeVC.setToRecipients(["[email protected]"])
composeVC.setSubject("My message")
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
@IBAction func sendPressed(_ sender: Any) {
guard MFMailComposeViewController.canSendMail() else {
showMailServiceErrorAlert()
return
}
composeVC.setMessageBody("Test credentials: \(firstAndLastNameTextField.text!)\nPhone: \(numberTextField.text!)\n\n\(messageTextView.text!)", isHTML: false)
self.present(composeVC, animated: true, completion: nil)
}
}
}
回答by Boris Y.
It wasn't possible for me to apply ?or?e's solution,this other answerhelped me.
我无法应用?or?e 的解决方案,这个其他答案帮助了我。
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult,
error: Swift.Error?) {
controller.dismiss(animated: true, completion: nil)
}
Adding Swift.
prefix to Error?
solves the problem.
添加Swift.
前缀来Error?
解决问题。
回答by jeffjv
I had the same issue where the mailComposeControler
delegate wasn't getting called after canceling or sending the mail. xCode gave me the same warning about adding private. I did not have an issue an enumeration named Error as the others had.
我遇到了同样的问题,mailComposeControler
即在取消或发送邮件后没有呼叫代表。xCode 给了我关于添加私有的相同警告。我没有像其他人那样遇到名为 Error 的枚举问题。
The only way I could fix it was to specifically define the function as public
.
我可以修复它的唯一方法是将函数明确定义为public
.
public func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult, error: Error?) {
// Check the result or perform other tasks.
// Dismiss the mail compose view controller.
self.dismiss(animated: true, completion: nil)
}
After doing that it worked fine.
这样做之后,它工作得很好。
This was in Swift 4.0, xCode 10.1.
这是在 Swift 4.0,xCode 10.1 中。
回答by ?or?e Nilovi?
EDIT:
编辑:
After adding other classes to the project I encountered the same problem again and realized that conversion was not the problem.
在项目中添加其他类后,我再次遇到同样的问题,并意识到转换不是问题。
The problem was that I have an enumeration named Error, that's why the parameter error: was not recognizing the Swift Error class, so the warning was correct.
问题是我有一个名为 Error 的枚举,这就是参数 error: 无法识别 Swift Error 类的原因,所以警告是正确的。
I came to edit the answer and I saw that Boris Y.wrote the fix for this, so I'll accept his answer.