2017-04-15 4 views
1

電子メールを作成し、ユーザーが送信するためにMFMailComposeViewControllerに表示するアプリケーションを構築しています。ユーザーがそれを送信またはキャンセルすると、アプリは適切な確認画面メッセージで応答します。Swift 3 MFMailComposeResult電子メール画面に基づいて確認画面を表示する方法

私は電子メールを作成し、作成画面を閉じることができました。私は、IBの名前のついたセグをプリコンポーズビューから確認ビューまで持っています。しかし、私はそのセグを実行することはできません。

したがって、どのようにしてセグの宛先のテキストメッセージを更新し、それにセグをすることができますか。

私はSwiftを学ぼうとしているので、コードの仕組みを理解することに非常に関心があります。だから私は本当に助けに感謝します。

ワークフローでは、アプリから写真を撮って、ユーザから始まる:この火災

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 

のとき:電子メールメッセージの内容のすべてを組み立て、それを渡す

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 

ユーザーは「キャンセル」の「送信」をタップします

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/mfmailcomponentsviewcontroller/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

したがって、mailComposeController補完ハンドラの中にあるcontroller.alert補完ハンドラの内部に警告コードを入れますか?私は補完ハンドラを入れ子にすることは考えていませんでした。ありがとう! – 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 comment "作成ビューが閉じると、確認ビューを表示する方法がわかりません" –

+1

@LeoDabusこのコメントの答えは正しい解決策です。 – Mannopson

+0

はい、これは動作しますが、作成画面が閉じる前に起動します。私は後に探しています。 – niblettes

関連する問題