gpt4 book ai didi

ios - Swift 5 - 电子邮件类助手/管理器

转载 作者:行者123 更新时间:2023-12-01 19:31:19 25 4
gpt4 key购买 nike

编辑:
非常感谢 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)
}
}
}
在 View Controller 中:
添加变量:
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)
}

我正在尝试创建一个类来处理初始化 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)
}
}
然后从不同的 View Controller 调用它:
  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() 不正确,那么该类显然无法初始化并崩溃。这是有道理的。
错误:
Unable to initialize due to + [MFMailComposeViewController canSendMail] returns NO.
我不确定如何从这里开始工作。我尝试将 FeedbackManager 从 MFMailComposeViewController 更改为 UIViewController。这似乎可行,但因为它在堆栈上添加了一个 View ,所以会导致奇怪的图形显示。
我可以做的另一件事是导入 MessageUI,并为我希望能够从中发送电子邮件的每个 Controller 符合 MFMailComposeViewController。这样我就可以在尝试初始化 FeedbackManager() 之前检查 canSendMail()。但这似乎也不是最好的答案。
我还能如何让这个工作?
编辑:
我已经得到了处理这个的代码,但是,在它呈现 MFMailComposeViewController 之前,将 View 添加到堆栈上会有一个丑陋的过渡。
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)
}
}

最佳答案

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

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))
}
}
}
然后从 View Controller 中使用它:

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直接禁用/隐藏允许他们发送反馈的 UI 组件

关于ios - Swift 5 - 电子邮件类助手/管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62617112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com