2017-04-15 20 views
1

我在构建一个应用程序,该应用程序构造一个电子邮件并将其呈现在MFMailComposeViewController中供用户发送。当用户发送或取消它时,我希望应用程序用适当的确认屏幕消息进行响应。Swift 3如何显示基于MFMailComposeResult电子邮件屏幕的确认屏幕

我可以编写电子邮件,解除撰写屏幕,并且我在IB中有一个从预编写视图到确认视图的已命名的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,并且它是正确的,但我无法弄清楚如何显示确认视图,以便在撰写视图滑过时可用。

+0

您需要为我们添加一些代码,以了解您尝试过的内容。我觉得应该看看['mailComposeDelegate'(https://developer.apple.com/reference/messageui/mfmailcomposeviewcontroller/1616890-mailcomposedelegate) –

回答

2

你需要让你的视图控制器MFMailComposeViewController代表和覆盖的方法didFinishWith结果和切换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") 
     } 
    } 
} 
+0

狮子座,谢谢。我知道了这一点,但我无法弄清楚如何在撰写视图关闭后呈现确认视图。 – niblettes

+0

因此,将警报代码放在controller.alert完成处理程序的内部,它位于mailComposeController完成处理程序的内部?我没有考虑嵌套完成处理程序。谢谢! – niblettes

+0

不客不雅 –

1

如果你想控制器解雇前提出警告?试试这个:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 


    switch result { 
    case .cancelled: 

     let alertController = UIAlertController.init(title: "Cancelled", message: "Some message", preferredStyle: .alert) 
     alertController.addAction(UIAlertAction.init(title: "Ok", style: .default, handler: { (alertAction) in 
      controller.dismiss(animated: true, completion: nil) 
     })) 
     controller.present(alertController, animated: true, completion: nil) 

    case .sent: 

     let alertController = UIAlertController.init(title: "Sent", message: "Some message", preferredStyle: .alert) 
     alertController.addAction(UIAlertAction.init(title: "Ok", style: .default, handler: { (alertAction) in 
      controller.dismiss(animated: true, completion: nil) 
     })) 
     controller.present(alertController, animated: true, completion: nil) 

    default: 
     break; 
    } 
} 
+0

OPs评论“我无法弄清楚如何在撰写视图关闭后呈现确认视图” –

+1

@LeoDabus对于此评论,您的回答是正确的解决方案。 – Mannopson

+0

是的,这个方法可行,但在撰写视图关闭之前它会触发。我在寻找。 – niblettes