どうも、ささおです!
今回はmailerをアプリから起動する方法をメモしていきます。
Contents
- 1 準備
- 2 実装
- 3 mailer起動関数作成
- 3.1 import MessageUI
- 3.2 @objc func startMailer(sender: UIButton) {}
- 3.2.1 MFMailComposeViewController()
- 3.2.2 let toRecipients = [“toSasao@ggmail.com”]
- 3.2.3 let CcRecipients = [“ccSasao@ggmail.com”,”ccSasao2@ggmail.com”]
- 3.2.4 let BccRecipients = [“BccSasao@ggmail.com”,”BccSasao2@ggmail.com”]
- 3.2.5 setSubject(“件名”)
- 3.2.6 setMessageBody(“Mailerイケイケ!Yeah!!”, isHTML: false)
- 3.2.7 present(mailViewController, animated: true, completion: nil)
- 4 確認
- 5 エラーが出てしまう場合
準備
mailerを起動するときには「MessageUI」というフレームワークを使用します。
まずはMessageUIを入れていきましょう。
- navigationエリアの一番上を選択
- TARGETS を選択
- Links frameworks and Librarysを選択
- 検索バーでMessageUIを検索して選択
- add
実装
今回は
ボタンをタップするとmailerが起動するという流れにします。
まずはUIButtonを作成し、配置しましょう!
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let startMailerBtn = UIButton(frame: CGRect(x: 0,y: 0,width: 200,height: 30)) startMailerBtn.layer.position = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2) startMailerBtn.setTitle("メールアプリ起動!", for: .normal) startMailerBtn.setTitleColor(UIColor.white, for: .normal) startMailerBtn.backgroundColor = UIColor.blue startMailerBtn.addTarget(self, action: #selector(ViewController.startMailer(sender:)), for: .touchUpInside) self.view.addSubview(startMailerBtn) } @objc func startMailer(sender: UIButton) { // ここにmailer起動の処理を記述する } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
画面のレイアウトはここで終了です。
このようになっています。
「UIButtonのコード謎!」という方はこちらの記事をご覧ください!
Swift:UIButtonをコードだけで実装するメモ!addTargetの使い方!

mailer起動関数作成
このようになります!
import UIKit import MessageUI class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let startMailerBtn = UIButton(frame: CGRect(x: 0,y: 0,width: 200,height: 30)) startMailerBtn.layer.position = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2) startMailerBtn.setTitle("メールアプリ起動!", for: .normal) startMailerBtn.setTitleColor(UIColor.white, for: .normal) startMailerBtn.backgroundColor = UIColor.blue startMailerBtn.addTarget(self, action: #selector(ViewController.startMailer(sender:)), for: .touchUpInside) self.view.addSubview(startMailerBtn) } @objc func startMailer(sender: UIButton) { let mailViewController = MFMailComposeViewController() let toRecipients = ["toSasao@ggmail.com"] let CcRecipients = ["ccSasao@ggmail.com","ccSasao2@ggmail.com"] let BccRecipients = ["BccSasao@ggmail.com","BccSasao2@ggmail.com"] mailViewController.setSubject("件名") mailViewController.setToRecipients(toRecipients) //Toアドレスの表示 mailViewController.setCcRecipients(CcRecipients) //Ccアドレスの表示 mailViewController.setBccRecipients(BccRecipients) //Bccアドレスの表示 mailViewController.setMessageBody("Mailerイケイケ!Yeah!!", isHTML: false) present(mailViewController, animated: true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
import MessageUI
MessageUIを使用するのでこちらを記述する必要があります。
@objc func startMailer(sender: UIButton) {}
関数の内容を説明していきます。
MFMailComposeViewController()
これがmailer画面のViewControllerです。
これを色々カスタムしていきます。
let toRecipients = [“toSasao@ggmail.com”]
配列toRecipientsはメールを送る先(アドレス)が入っています。
setToRecipientsの引数に入れることでmailer画面でアドレスが表示されます。
mailViewController.setToRecipients(toRecipients) //Toアドレスの表示
let CcRecipients = [“ccSasao@ggmail.com”,”ccSasao2@ggmail.com”]
CCは、TOで送るのとの違い、
TOで送る人以外へ向けたもので、
「確認のために見てください!」「念のためにお送りします!」といった意味合いが含まれています。
こちらもsetCcRecipientsの引数に入れることでmailer画面でアドレスが表示されます。
mailViewController.setCcRecipients(CcRecipients) //Ccアドレスの表示
let BccRecipients = [“BccSasao@ggmail.com”,”BccSasao2@ggmail.com”]
BCCは、TO.CCで送るのと違い、
BCCは一斉送信する際などの、
アドレスを隠したいときに使用します。
こちらもsetBccRecipientsの引数に入れることでmailer画面でアドレスが表示させます。
mailViewController.setBccRecipients(BccRecipients) //Bccアドレスの表示
setSubject(“件名”)
subject、つまり件名を指定することができます。
今回はシンプルに件名に”件名”と表示させます。
setMessageBody(“Mailerイケイケ!Yeah!!”, isHTML: false)
MessageBodyには本文を入力します。
“Mailerイケイケ!Yeah!!”とイケイケにしてみました。
isHTMLはfalseにしています。
trueにすると本文にHTMLを適用することができます。
つまり、
setMessageBody(“<p style=\”color:blue;\”>Mailerイケイケ!Yeah!!</p>“, isHTML: true)
のようにすることも可能です。
この場合本文の文字が青くなります。
present(mailViewController, animated: true, completion: nil)
ここでpresentでmailerを起動します。
このままでもメールを送ることができますが、
送ったりしても、元の画面に戻ってくる手段がないです。
ちゃんと戻ってこられるようにしましょう。
import UIKit import MessageUI class ViewController: UIViewController, MFMailComposeViewControllerDelegate { override func viewDidLoad() { super.viewDidLoad() let startMailerBtn = UIButton(frame: CGRect(x: 0,y: 0,width: 200,height: 30)) startMailerBtn.layer.position = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2) startMailerBtn.setTitle("メールアプリ起動!", for: .normal) startMailerBtn.setTitleColor(UIColor.white, for: .normal) startMailerBtn.backgroundColor = UIColor.blue startMailerBtn.addTarget(self, action: #selector(ViewController.startMailer(sender:)), for: .touchUpInside) self.view.addSubview(startMailerBtn) } @objc func startMailer(sender: UIButton) { let mailViewController = MFMailComposeViewController() let toRecipients = ["toSasao@ggmail.com"] let CcRecipients = ["ccSasao@ggmail.com","ccSasao2@ggmail.com"] let BccRecipients = ["BccSasao@ggmail.com","BccSasao2@ggmail.com"] mailViewController.mailComposeDelegate = self mailViewController.setSubject("件名") mailViewController.setToRecipients(toRecipients) //Toアドレスの表示 mailViewController.setCcRecipients(CcRecipients) //Ccアドレスの表示 mailViewController.setBccRecipients(BccRecipients) //Bccアドレスの表示 mailViewController.setMessageBody("Mailerイケイケ!Yeah!!", isHTML: false) present(mailViewController, animated: true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { switch result { case .cancelled: print("キャンセルしました") case .saved: print("セーブしました") case .sent: print("送信しました") case .failed: print("失敗しました。") } self.dismiss(animated: true, completion: nil) } }
これで戻ってこられるようになりました!!
確認
びっくりですが、
実機でないと確認できないみたいです・・・
エラーが出てしまう場合
Filtering mail sheet accounts for bundle ID: ?????????????, source account management: 1
僕はこのようなエラーが出てしまって呻いていました。
この場合、iPhone標準のメールアプリでアカウント登録できていない可能性があります。
僕はgmailのアカウントで登録したらうまくいきました。