###前提・実現したいこと
現在、ユーザー投稿型のiOSアプリを作成していて、投稿の違反報告機能の実装を行なっています。
メール画面を起動して送信しているのですが、送信した後に閉じることができません。
送信したり下書き保存したりはできています。
ググってみると
swift
1 func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 2 // Dismiss the mail compose view controller. 3 controller.dismiss(animated: true, completion: nil) 4 }
このコードで閉じることができるようなのですが、私の環境ですと一向にこの関数が呼ばれません。
ViewControllerをdelegateしているカスタムセルクラス内で下記のように行なっています。
swift
1 let reportPost = UIAlertAction(title: "違反を報告する", style: .default, handler: { 2 (action: UIAlertAction) -> Void in 3 let reportSheet = UIAlertController(title:"メールで報告", message:"報告内容を入力してください。送信ボタンを押すとメールアプリが起動されます。", preferredStyle: .alert) 4 reportSheet.addTextField(configurationHandler: { textField in 5 textField.placeholder = "報告内容" 6 textField.clearButtonMode = .whileEditing 7 }) 8 9 let reportAction = UIAlertAction(title: "送信", style: .default, handler: { 10 (action: UIAlertAction) -> Void in 11 // 報告する 12 13 let textField = reportSheet.textFields![0] 14 15 let mailViewController = MFMailComposeViewController() 16 let toRecipients = ["〇〇@gmail.com"] 17 18 mailViewController.setSubject("違反報告") 19 mailViewController.setToRecipients(toRecipients) //Toアドレスの表示 20 mailViewController.setMessageBody("ご報告いただきありがとうございます。\n報告内容を確認の上、適切に対応させていただきます。このメールは編集せずこのまま送信してください。\n今後ともよろしくお願いいたします。\n運営チーム\n\nご報告内容: (textField.text ?? "なし")\n該当の俳句ID: (self.postID.text ?? "エラー")", isHTML: false) 21 22 self.delegate!.present(mailViewController, animated: true, completion: nil) 23 }) 24 25 let cancelReport = UIAlertAction(title: "やめる", style: .cancel, handler: { 26 (action: UIAlertAction) -> Void in 27 // キャンセル 28 }) 29 30 reportSheet.addAction(reportAction) 31 reportSheet.addAction(cancelReport) 32 33 self.delegate!.present(reportSheet, animated: true, completion: nil) 34 }) 35
###試したこと
swift
1 func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 2 // Dismiss the mail compose view controller. 3 controller.dismiss(animated: true, completion: nil) 4 }
最初にあげたこのコードをカスタムセルクラス内と表示しているViewController内のそれぞれに記述してみましたがどちらの場合も呼ぶことができませんでした。
このコードがそもそも間違っているのか、または記述する場所を間違えているのか、ご指摘いただければと思います。何卒よろしくお願い申し上げます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/12/09 10:55
2017/12/09 11:12