Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

Swift 3 How to display a confirmation screen based on MFMailComposeResult email screen

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.

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.

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

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...