0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】お問い合わせ機能を作る アプリ内でメーラー起動する

Posted at

概要

iOSアプリ内でメールを送信する機能を作る手順をまとめる。

目次

  • フレームワークを追加
  • ソースコードを書く
    1. MessageUIをimportする
    2. MFMailComposeViewControllerインスタンスを生成し、宛先や本文などを設定する
    3. MFMailComposeViewControllerDelegateで終了処理を実装する

フレームワークを追加

MessageUI.frameworkを追加する

プロジェクト > Build Phases > Link Binary With Libraries > 「+」> MessageUI.framework

ソースコードを書く

1. MessageUIをimportする

import MessageUI

2. MFMailComposeViewControllerインスタンスを生成する

今回はテーブルビューセルがタップされた場合

		case 2:
            // お問い合わせ機能
            if MFMailComposeViewController.canSendMail() {
                let mail = MFMailComposeViewController()
                mail.mailComposeDelegate = self
                mail.setToRecipients(["paciolist@gmail.com"])   // 宛先アドレス
                mail.setSubject("問い合わせ")                     // 件名
                mail.setMessageBody("", isHTML: false)          // 本文
                present(mail, animated: true, completion: nil)
            }
            else {
                print("送信できません")
            }
        break

3. MFMailComposeViewControllerDelegateで終了処理を実装する

extension SettingsTableViewController: MFMailComposeViewControllerDelegate {
    // お問い合わせ機能
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        
        switch result {
        case .cancelled:
            print("キャンセル")
        case .saved:
            print("下書き保存")
        case .sent:
            print("送信成功")
        default:
            print("送信失敗")
        }
        
        dismiss(animated: true, completion: nil)
    }
}

参考

【Swift】 お問い合わせ機能の実装
https://qiita.com/_mkt_/items/fd73c10dcc5a760149e3

【Swift】アプリ内でメーラーを起動しメール送信するやり方
https://qiita.com/kobaboy/items/60a2f44ad53675cfa0ae

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?