Swift 5 - 电子邮件 Class 助手/经理

Swift 5 - Email Class Helper / Manager

编辑:

非常感谢 Paulw11 帮我解决了这个问题。我在这里添加了完整的代码以便于重用:

Class:

import UIKit
import MessageUI

struct Feedback {
    let recipients: [String]
    let subject: String
    let body: String
    let footer: String
}

class FeedbackManager: NSObject, MFMailComposeViewControllerDelegate {

private var feedback: Feedback

private var completion: ((Result<MFMailComposeResult,Error>)->Void)?

override init() {
    fatalError("Use FeedbackManager(feedback:)")
}

init?(feedback: Feedback) {
    guard MFMailComposeViewController.canSendMail() else {
        return nil
    }
    
    self.feedback = feedback
}

func send(on viewController: UIViewController, completion:(@escaping(Result<MFMailComposeResult,Error>)->Void)) {
    
    let mailVC = MFMailComposeViewController()
    self.completion = completion
    
    mailVC.mailComposeDelegate = self
    mailVC.setToRecipients(feedback.recipients)
    mailVC.setSubject(feedback.subject)
    mailVC.setMessageBody("<p>\(feedback.body)<br><br><br><br><br>\(feedback.footer)</p>", isHTML: true)
    
    viewController.present(mailVC, animated:true)
}

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    if let error = error {
        completion?(.failure(error))
        controller.dismiss(animated: true)
    } else {
        completion?(.success(result))
        controller.dismiss(animated: true)
    }
}
}

在视图控制器中:

添加变量:

var feedbackManager: FeedbackManager?

使用:

    let feedback = Feedback(recipients: "String", subject: "String", body: "Body", footer: "String")
    if let feedManager = FeedbackManager(feedback: feedback) {
        self.feedbackManager = feedManager
        self.feedbackManager?.send(on: self) { [weak self] result in
            switch result {
            case .failure(let error):
                print("error: ", error.localizedDescription)
            // Do something with the error
            case .success(let mailResult):
                print("Success")
                // Do something with the result
            }
            self?.feedbackManager = nil
        }
    } else { // Cant Send Email: // Added UI Alert:
        let failedMenu = UIAlertController(title: "String", message: nil, preferredStyle: .alert)
        let okAlert = UIAlertAction(title: "String", style: .default)
        failedMenu.addAction(okAlert)
        present(failedMenu, animated: true)
    }

我正在尝试制作一个 class 来处理初始化 MFMailComposeViewController 以在应用程序内部发送电子邮件。

我在使用时遇到了问题。好吧,如果它 工作,而不是让它崩溃。

class:

import UIKit
import MessageUI

struct Feedback {
    let recipients = "String"
    let subject: String
    let body: String
}

class FeedbackManager: MFMailComposeViewController, MFMailComposeViewControllerDelegate {
    
    func sendEmail(feedback: Feedback) {
        
        if MFMailComposeViewController.canSendMail() {
            
            self.mailComposeDelegate = self
            self.setToRecipients([feedback.recipients])
            self.setSubject("Feedback: \(feedback.subject)")
            self.setMessageBody("<p>\(feedback.body)</p>", isHTML: true)
            
        } else {
            print("else:")
            mailFailed()
        }
    }
    
    func mailFailed() {
        print("mailFailed():")
        let failedMenu = UIAlertController(title: "Please Email Me!", message: nil, preferredStyle: .alert)
        let okAlert = UIAlertAction(title: "Ok!", style: .default)
        failedMenu.addAction(okAlert)
        self.present(failedMenu, animated: true, completion: nil)
    }
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
    }
}

然后从不同的视图控制器调用它:

  let feedbackManager = FeedbackManager()
  feedbackManager.sendEmail(feedback: Feedback(subject: "String", body: "String"))
  self.present(feedbackManager, animated: true, completion: nil)
  tableView.deselectRow(at: indexPath, animated: true)

如果 MFMailComposeViewController.canSendMail() == true,上面的代码工作得很好。我面临的问题是,如果 canSendMail() 不正确,那么 class 显然无法初始化并崩溃。这是有道理的。

错误:

Unable to initialize due to + [MFMailComposeViewController canSendMail] returns NO.

我不确定从这里开始如何让它工作。我尝试将 FeedbackManager 从 MFMailComposeViewController 更改为 UIViewController。这似乎可行,但因为它在堆栈上添加了一个视图,所以导致了奇怪的图形显示。

我可以做的另一件事是导入 MessageUI,并为我希望能够从中发送电子邮件的每个控制器遵循 MFMailComposeViewController。这样我就可以在尝试初始化 FeedbackManager() 之前检查 canSendMail()。但这似乎也不是最佳答案。

我还能怎样让它工作?

编辑: 我已经得到了处理这个的代码,但是,在它呈现 MFMailComposeViewController 之前,将视图添加到堆栈上有一个丑陋的过渡。

class FeedbackManager: UIViewController, MFMailComposeViewControllerDelegate {
    
    func sendEmail(feedback: Feedback, presentingViewController: UIViewController) -> UIViewController {
        
        if MFMailComposeViewController.canSendMail() {
            
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setToRecipients([feedback.recipients])
            mail.setSubject("Feedback: \(feedback.subject)")
            mail.setMessageBody("<p>\(feedback.body)</p>", isHTML: true)
            
             present(mail, animated: true)
            return self
        } else {
            print("else:")
            return mailFailed(presentingViewController: presentingViewController)
        }
    }
    
    func mailFailed(presentingViewController: UIViewController) -> UIViewController {
        print("mailFailed():")
        let failedMenu = UIAlertController(title: "Please Email Me!", message: nil, preferredStyle: .alert)
        let okAlert = UIAlertAction(title: "Ok!", style: .default)
        failedMenu.addAction(okAlert)
        return failedMenu
    }
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
        self.dismiss(animated: false)
    }
}

您可以按如下方式更改代码。

struct Feedback {
    let recipients = "String"
    let subject: String
    let body: String
}

class FeedbackManager: NSObject, MFMailComposeViewControllerDelegate {
    
    func sendEmail(presentingViewController: UIViewController)) {
        
        if MFMailComposeViewController.canSendMail() {
            
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setToRecipients([feedback.recipients])
            mail.setSubject("Feedback: \(feedback.subject)")
            mail.setMessageBody("<p>\(feedback.body)</p>", isHTML: true)
            presentingViewController.present(mail, animated: true)
            
        } else {
            print("else:")
            mailFailed(presentingViewController: presentingViewController)
        }
    }
    
    func mailFailed(presentingViewController: UIViewController) {
        print("mailFailed():")
        let failedMenu = UIAlertController(title: "Please Email Me!", message: nil, preferredStyle: .alert)
        let okAlert = UIAlertAction(title: "Ok!", style: .default)
        failedMenu.addAction(okAlert)
        presentingViewController.present(failedMenu, animated: true, completion: nil)
    }
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
    }
}

现在,mailComposer 可以从另一个 UIViewController class.

打开,如下所示
let feedbackManager = FeedbackManager()
feedbackManager.sendEmail(presentingViewController: self)

希望对您有所帮助

通过首先添加一个 class 检查 .canSendMail 是否为真解决了这个问题。如果是,则它会利用邮政发送 class 来呈现 MFMailComposeViewController。

这是我想出的唯一解决方法,它允许 MFMailComposeViewController 成为它自己的 MFMailComposeViewControllerDelegate。同时如果 .canSendMail = false.

也可以防止崩溃
import UIKit
import MessageUI

struct Feedback {
    let recipients = ["Strings"]
    let subject: String
    let body: String
}

class FeedbackManager {

    func tryMail() -> Bool {
        if MFMailComposeViewController.canSendMail() {
            return true
        } else {
            return false
        }
    }

    func mailFailed() -> UIViewController {
        let failedMenu = UIAlertController(title: "Please Email Me!", message: nil, preferredStyle: .alert)
        let okAlert = UIAlertAction(title: "Ok!", style: .default)
        failedMenu.addAction(okAlert)
        return failedMenu
    }
}

class PostalManager: MFMailComposeViewController, MFMailComposeViewControllerDelegate {

    func sendEmail(feedback: Feedback) -> MFMailComposeViewController {
        if MFMailComposeViewController.canSendMail() {
            self.mailComposeDelegate = self
            self.setToRecipients(feedback.recipients)
            self.setSubject("Feedback: \(feedback.subject)")
            self.setMessageBody("<p>\(feedback.body)</p>", isHTML: true)
        }
        return self
    }

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

调用方式:

let feedbackManager = FeedbackManager()
let feedback = Feedback(subject: "String", body: "Body")

switch feedbackManager.tryMail() {

case true:
let postalManager = PostalManager()
present(postalManager.sendEmail(feedback: feedback), animated: true)
case false:
present(feedbackManager.mailFailed(), animated: true)
}

Subclassing MFMailComposeViewController 是错误的方法。此 class 旨在“按原样”使用。如果你愿意,你可以构建一个包装器 class:

struct Feedback {
    let recipients = "String"
    let subject: String
    let body: String
}

class FeedbackManager: NSObject, MFMailComposeViewControllerDelegate {
    
    private var feedback: Feedback
    
    private var completion: ((Result<MFMailComposeResult,Error>)->Void)?
    
    override init() {
        fatalError("Use FeedbackManager(feedback:)")
    }
    
    init?(feedback: Feedback) {
        guard MFMailComposeViewController.canSendMail() else {
            return nil
        }
        
        self.feedback = feedback
    }
    
    func send(on viewController: UIViewController, completion:(@escaping(Result<MFMailComposeResult,Error>)->Void)) {
        
        let mailVC = MFMailComposeViewController()
        self.completion = completion
        
        mailVC.mailComposeDelegate = self
        mailVC.setToRecipients([feedback.recipients])
        mailVC.setSubject("Feedback: \(feedback.subject)")
        mailVC.setMessageBody("<p>\(feedback.body)</p>", isHTML: true)
        
        viewController.present(mailVC, animated:true)
    } 
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        if let error = error {
            completion?(.failure(error))
        } else {
            completion?(.success(result))
        }
    }
}

然后从视图控制器中使用它:


let feedback = Feedback(subject: "String", body: "Body")
if let feedbackMgr = FeedbackManager(feedback: feedback) {
    self.feedbackManager = feedbackMgr
    feedback.send(on: self) { [weak self], result in 
        switch result {
            case .failure(let error):
                // Do something with the error
            case .success(let mailResult):
                // Do something with the result
        }
        self.feedbackManager = nil
    }
} else {
    // Can't send email
}

您需要在 属性 中持有对 FeedbackManager 的强引用,否则它会在包含函数退出时立即释放。我上面的代码指的是 属性

var feedbackManager: FeedbackManager?

虽然这会起作用,但更好的用户体验是,如果您直接检查 canSendMail 和 disable/hide 允许他们发送反馈的 UI 组件