Swift 3 如何显示基于 MFMailComposeResult 电子邮件屏幕的确认屏幕 [英] Swift 3 How to display a confirmation screen based on MFMailComposeResult email screen

查看:25
本文介绍了Swift 3 如何显示基于 MFMailComposeResult 电子邮件屏幕的确认屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building an app that constructs an email and presents it in a MFMailComposeViewController for the user to send. When the user sends or cancels it I want the app to respond with an appropriate confirmation screen message.

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

I'm able to compose the email, dismiss the compose screen, and I have a named segue in IB from the pre-compose view to the confirmation view. But I cannot get that segue to execute.

那么,我如何更新 segue 目标中的文本消息,然后转到它.

So, how can I update text message in the segue destination and then segue to it.

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

Because I'm trying to learn Swift I'm very interested in understanding how the code works, not just getting code that works. So I really do appreciate any help.

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

The workflow starts with the user snapping a photo from the app:

    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

它组合了所有电子邮件内容并将其传递给:

Which assembles all of the email message content and passes it to:

    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

然后当用户点击取消"的发送"时触发

And then this fires when the user taps "Send" of "Cancel"

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,它是正确的,但我无法弄清楚如何呈现确认视图,以便在撰写视图滑开时它可用.

And this is where I find myself stumped. I can access MFMailComposeResult, and it is correct, but I cannot figure out how to present the confirmation view so it is available as the compose view slides away.

推荐答案

您需要使您的视图控制器成为 MFMailComposeViewController 委托并覆盖方法 didFinishWith result 并在解除方法的完成处理程序中切换 MFMailComposeResult 值:

You need to make your view controller the MFMailComposeViewController delegate and override the method didFinishWith result and switch MFMailComposeResult value inside the completion handler of the dismiss method :

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 电子邮件屏幕的确认屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆