2017-04-15 53 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