2017年2月27日月曜日

メールの送信をしてみる

MFMailComposeViewController を使うと、
- Swiftのアプリで送信先・題名・本文を生成
- メール送信アプリで送信(iPhoneなどのアカウントで)
という動作ができるようです。
(androidのintentのような感じ)


準備 : MessageUI.frameworkをlink libraryに追加
1. プロジェクトを選択
2. Build Phasesタブ -> Link Binary With Libralies
3. MessageUI.frameworkを選択

コード上のポイント
- ViewControllerに MFMailComposeViewControllerDelegate を追加
- viewDidLoadedに
 composeVC = MFMailComposeViewController() を追加
- composeVCに対して、to/cc/bcc/subject/bodyを追加する
- self.presentViewController(composeVC, animated: true, completion: nil) でメール画面に遷移
- delegateに以下を追加
 self.dismissViewControllerAnimated(true, completion: nil)

---
import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
       
        // メール対応
        let composeVC = MFMailComposeViewController()
        composeVC.mailComposeDelegate = self
   
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func buttonA(sender: AnyObject) {
        makeFixedForm("本文a")
    }
   
   
    @IBAction func buttonB(sender: AnyObject) {
        makeFixedForm("本文b")
    }
   
    // メール定型文書作成
    func makeFixedForm(subj:String){
        let toRecipients = ["送信先1", "送信先2"]
        let ccRecipients = ["cc送信先"]
        let bccRecipients = [""]
        let subject = subj
        let messageBody = "本文です"
       
        sendMail(toRecipients, cc: ccRecipients, bcc: bccRecipients, subj: subject, msg: messageBody)
    }
   
    // 終了デリゲート
    func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
        if result == MFMailComposeResultCancelled {
            print("canceld")
        } else if result == MFMailComposeResultSaved {
            print("saved")
        } else if result == MFMailComposeResultSent {
            print("sent")
        } else if result == MFMailComposeResultFailed {
            print("failed")
        }
        self.dismissViewControllerAnimated(true, completion: nil)
    }
   
    // 引数の内容でメールアプリを起動する
    func sendMail(to:[String], cc:[String], bcc:[String], subj:String, msg: String){
        if !MFMailComposeViewController.canSendMail() {
            print("Mail services are not available")
            return
        }
        let composeVC = MFMailComposeViewController()
        composeVC.mailComposeDelegate = self
       
        var toRecipients:[String] = []
        var ccRecipients:[String] = []
        var bccRecipients:[String] = []
        var subject:String
        var messageBody:String
       
        // TODO:ちゃんとチェックする事
        if(to.count != 0){
            for toMember in to {
                toRecipients.append(toMember)
            }
        }
        if(cc.count != 0){
            for ccMember in cc {
                ccRecipients.append(ccMember)
            }
        }
        if(bcc.count != 0){
            for bccMember in bcc {
                bccRecipients.append(bccMember)
            }
        }
        subject = subj
        messageBody = msg
       
        // Configure the fields of the interface.
        /*
        composeVC.setToRecipients(["送信先1", "送信先2"]
        composeVC.setBccRecipients(["cc送信先"])
        composeVC.setSubject("こんにちわ!")
        composeVC.setMessageBody("Hello from California!", isHTML: false)
        */
       
        if(toRecipients.count > 0){
            composeVC.setToRecipients(toRecipients)
        }
        if(ccRecipients.count > 0){
            composeVC.setCcRecipients(ccRecipients)
        }
        if(bccRecipients.count > 0){
            composeVC.setBccRecipients(bccRecipients)
        }
        if(subject.characters.count > 0){
            composeVC.setSubject(subject)
        }
        if(messageBody.characters.count > 0){
            composeVC.setMessageBody(messageBody, isHTML:false)
        }
       
        // Present the view controller modally.
        self.presentViewController(composeVC, animated: true, completion: nil)
    }
}

2017年2月26日日曜日

配列の使い方と配列を引数とする関数

最も単純な配列を使う例。

カラの配列を作る
メンバを追加する
for inでメンバを取り出す

という最も単純な使い方です

import UIKit

var str = "Hello, playground"

var members:[String] = []   // 空の配列を作って
members.append("sara") // メンバを追加していく
members.append("zara")
members.append("bis")

for member in members {
    print(member)         // 配列の中身を表示してみる
}

print("配列を引数とする関数の定義と受け渡し")
func dispArray(team:[String]) { // 配列を引数として
    for teamMate in team{
        print(teamMate)         // 中身を表示
    }
}

members.append("duka")  // メンバを追加してみる
dispArray(members)      // 配列を引数として上の表示関数を呼んでみる