gpt4 book ai didi

Swift 3 如何显示基于 MFMailComposeResult 电子邮件屏幕的确认屏幕

转载 作者:行者123 更新时间:2023-11-30 11:38:10 24 4
gpt4 key购买 nike

我正在构建一个应用程序,该应用程序可以构造电子邮件并将其呈现在 MFMailComposeViewController 中以供用户发送。当用户发送或取消它时,我希望应用程序以适当的确认屏幕消息进行响应。

我能够撰写电子邮件,关闭撰写屏幕,并且在 IB 中从预撰写 View 到确认 View 有一个命名的 segue。但我无法执行该segue。

那么,我怎样才能更新segue目的地中的短信,然后segue到它。

因为我正在尝试学习 Swift,所以我对了解代码的工作原理非常感兴趣,而不仅仅是获得有效的代码。所以我真的很感谢任何帮助。

工作流程从用户从应用中拍摄照片开始:

    func snapPhoto(){

if let cameraConnection = sessionOutput.connection(withMediaType: AVMediaTypeVideo) {

sessionOutput.captureStillImageAsynchronously(from: cameraConnection, completionHandler: { buffer, error in

let myMessage = self.buildEmail()
let myJpg = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
let mapSnap = (self.myMap == nil) ? nil : UIImagePNGRepresentation(self.myMap!)

let mail = self.setupMail(to: myMessage.to, subject: myMessage.subject, body: myMessage.body, myJpg: myJpg!, mapSnap: mapSnap)

self.presentMailComposer(mail: mail)

}) // close completionHandler

} // close if let cameraConnection

} // close func snapPhoto

它将所有电子邮件内容组合起来并将其传递到:

    func presentMailComposer(mail : MFMailComposeViewController) {

if MFMailComposeViewController.canSendMail() {

self.present(mail, animated: true, completion: nil)

} else {

let sendMailErrorAlert = UIAlertController.init(title: "Uh oh!", message: "Unable to send email.", preferredStyle: .alert)
self.present(sendMailErrorAlert, animated: true, completion: nil)

} // close if

} // close presentEmailComposer

然后当用户点击“发送”或“取消”时触发

public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {

switch result.rawValue {

case MFMailComposeResult.cancelled.rawValue:

self.performSegue(withIdentifier: "afterEmail", sender: self)
print("Mail cancelled")

case MFMailComposeResult.saved.rawValue:

print("Mail saved")

case MFMailComposeResult.sent.rawValue:

print("Mail sent")

case MFMailComposeResult.failed.rawValue:

print("Mail sent failure: %@", [error!.localizedDescription])

default:

break

}

controller.dismiss(animated: true, completion: nil)

} // close mailCompose

这就是我发现自己陷入困境的地方。我可以访问 MFMailComposeResult,它是正确的,但我不知道如何呈现确认 View ,以便在撰写 View 滑开时可用。

最佳答案

您需要使 View Controller 成为 MFMailComposeViewController 委托(delegate),并重写 didFinishWith 结果方法,并在解雇方法的完成处理程序中切换 MFMailComposeResult 值:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true) {
// do whatever you need to do after dismissing the mail window
switch result {
case .cancelled: print("cancelled")
case .saved: print("saved")
case .sent:
let alert = UIAlertController(title: "Mail Composer", message: "Mail was successfully sent", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Done", style: .default, handler: nil))
self.present(alert, animated: true)
case .failed: print("failed")
}
}
}

关于Swift 3 如何显示基于 MFMailComposeResult 电子邮件屏幕的确认屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49520765/

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