我正在构建一个应用程序,该应用程序构造电子邮件并将其呈现在MFMailComposeViewController中,以供用户发送。当用户发送或取消它时,我希望该应用以适当的确认屏幕消息作为响应。
我可以撰写电子邮件,关闭撰写屏幕,并且在IB中从预先撰写视图到确认视图都有一个命名的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,它是正确的,但是我无法弄清楚如何显示确认视图,因此在撰写视图滑动时可以使用该视图。
您需要使视图控制器成为MFMailComposeViewController委托,并覆盖didFinishWith result方法,并在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") } } }