gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:34:14 24 4
gpt4 key购买 nike

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

我能够撰写电子邮件,关闭撰写屏幕,并且我在 IB 中有一个从预撰写 View 到确认 View 的命名 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 结果并在 dismiss 方法的完成处理程序中切换 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/43431901/

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