URLスキームによるメーラーの呼び出し
Swift3.0
//
// ViewController.swift
// Utility007_3.0
//
// Created by KimikoWatanabe on 2016/08/24.
// Copyright © 2016年 FaBo, Inc. All rights reserved.
//
Swift 2.3
//
// ViewController.swift
// Utility007_2.3
//
// Created by KimikoWatanabe on 2016/08/24.
// Copyright © 2016年 FaBo, Inc. All rights reserved.
//
import UIKit
import SystemConfiguration
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//UIボタンを生成.
let myButton = UIButton(frame: CGRectMake(0,0,100,100))
myButton.backgroundColor = UIColor.redColor()
myButton.layer.masksToBounds = true
myButton.setTitle("メール", forState: .Normal)
myButton.layer.cornerRadius = 50.0
myButton.layer.position = CGPoint(x: self.view.bounds.width/2, y: self.view.bounds.height/2)
myButton.addTarget(self, action: #selector(ViewController.onClickMyButton(_:)), forControlEvents: .TouchUpInside)
self.view.addSubview(myButton)
}
//ボタンイベント.
func onClickMyButton(sender: UIButton){
//メーラーのURLを生成.
let myMailURL : NSURL = NSURL(string:"mailto:[email protected]")!
//URLスキームからメーラーを呼び出す.
UIApplication.sharedApplication().openURL(myMailURL)
}
}
2.3と3.0の差分
- UIColorの参照方法が変更(UIColor.grayColor()->UIColor.gray)
- CGRect,CGPointの初期化方法の変更(CGRectMake,CGPointMakeの廃止)
Reference
- SystemConfiguration Framework
- NSURL Class