MFMailComposeViewController,Swift 4,Xcode 9
MFMailComposeViewController, Swift 4, Xcode 9
我的 MFMailComposeViewControllerDelegate 函数有问题。
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
警告说
Instance method 'mailComposeController(:didFinishWith:error:)' nearly matches optional requirement 'mailComposeController(:didFinishWith:error:)' of protocol 'MFMailComposeViewControllerDelegate'
Make 'mailComposeController(_:didFinishWith:error:)' private to silence this warning
我需要 return 用户到应用程序并在单击取消后关闭 MFMailComposeViewController 并且不会触发此功能。
是的,我添加了委托composeVC.mailComposeDelegate = self
如果有人有类似的问题,我将不胜感激。谢谢
编辑
仅当我将语言设置为 Swift 4 时才会发生此行为。我刚刚返回了几次提交,它在 Swift 3.2
上运行得非常好
基本上,这是代码:
class TechSupportVC: UIViewController, MFMailComposeViewControllerDelegate {
let composeVC = MFMailComposeViewController()
override func viewDidLoad() {
super.viewDidLoad()
composeVC.mailComposeDelegate = self
composeVC.setToRecipients(["desiredEmail@gmail.com"])
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)
}
}
编辑:
向项目添加其他 classes 后,我再次遇到同样的问题,并意识到转换不是问题所在。
问题是我有一个名为 Error 的枚举,这就是为什么参数错误:无法识别 Swift 错误 class,所以警告是正确的。
我是来编辑答案的,我看到了Boris Y. wrote the fix for this, so I'll accept 。
我无法申请 this 帮助了我。
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult,
error: Swift.Error?) {
controller.dismiss(animated: true, completion: nil)
}
在 Error?
中添加 Swift.
前缀可以解决问题。
我遇到了同样的问题,即 mailComposeControler
委托在取消或发送邮件后没有被调用。 xCode 给了我关于添加 private 的相同警告。我没有像其他人一样遇到名为 Error 的枚举问题。
我唯一可以修复它的方法是将函数专门定义为 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)
}
这样做之后效果很好。
这是在 Swift 4.0、xCode 10.1 中。
我的 MFMailComposeViewControllerDelegate 函数有问题。
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
警告说
Instance method 'mailComposeController(:didFinishWith:error:)' nearly matches optional requirement 'mailComposeController(:didFinishWith:error:)' of protocol 'MFMailComposeViewControllerDelegate'
Make 'mailComposeController(_:didFinishWith:error:)' private to silence this warning
我需要 return 用户到应用程序并在单击取消后关闭 MFMailComposeViewController 并且不会触发此功能。
是的,我添加了委托composeVC.mailComposeDelegate = self
如果有人有类似的问题,我将不胜感激。谢谢
编辑
仅当我将语言设置为 Swift 4 时才会发生此行为。我刚刚返回了几次提交,它在 Swift 3.2
上运行得非常好基本上,这是代码:
class TechSupportVC: UIViewController, MFMailComposeViewControllerDelegate {
let composeVC = MFMailComposeViewController()
override func viewDidLoad() {
super.viewDidLoad()
composeVC.mailComposeDelegate = self
composeVC.setToRecipients(["desiredEmail@gmail.com"])
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)
}
}
编辑:
向项目添加其他 classes 后,我再次遇到同样的问题,并意识到转换不是问题所在。
问题是我有一个名为 Error 的枚举,这就是为什么参数错误:无法识别 Swift 错误 class,所以警告是正确的。
我是来编辑答案的,我看到了Boris Y. wrote the fix for this, so I'll accept
我无法申请
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult,
error: Swift.Error?) {
controller.dismiss(animated: true, completion: nil)
}
在 Error?
中添加 Swift.
前缀可以解决问题。
我遇到了同样的问题,即 mailComposeControler
委托在取消或发送邮件后没有被调用。 xCode 给了我关于添加 private 的相同警告。我没有像其他人一样遇到名为 Error 的枚举问题。
我唯一可以修复它的方法是将函数专门定义为 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)
}
这样做之后效果很好。
这是在 Swift 4.0、xCode 10.1 中。