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