追記:Swift3で、書き方が変わりました。Swift3はこちらを参照ください。
====================
アプリからメーラー起動させるときの調査。Objective-Cだから参考程度に見ました。
mailto:スキームの注意点(iOS)
これもObjective-C
http://www.kronos-jp.net/technologies/knowledge/knowledge-7/528.html
カスタムURLスキームを使ったアプリ間連携
※2018年2月リンク切れ
iosアプリをつくっていて、アプリからメーラーを起動させる実装をしているとき、
他のメーラーとか、そもそも別アプリの起動方法ってどうやるのだろと思って調べました。
URLスキームを呼んで起動させてあげるようです。
ただ、今回は、メーラ起動だったので、スキームは必要なくできました。
メーラ起動例
※ MessageUIフレームワークを追加しておくこと
import MessageUI
// Const.~~~は定数
// メーラー起動
@IBAction func touchDownSendMailBtn(sender: AnyObject) {
// メールを送信できるかチェック
if MFMailComposeViewController.canSendMail()==false {
print("Email Send Failed", terminator: "")
return
}
let mailViewController = MFMailComposeViewController()
let toRecipients = [Const.MailFrom]
mailViewController.mailComposeDelegate = self
// メール件名
mailViewController.setSubject(Const.MailSubject)
// Toアドレスの表示
mailViewController.setToRecipients(toRecipients)
// メール本文
mailViewController.setMessageBody(Const.MailBody, isHTML: false)
self.presentViewController(mailViewController, animated: true, completion: nil)
}
// メールキャンセル
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
switch result.rawValue {
case MFMailComposeResultCancelled.rawValue:
print("Email Send Cancelled", terminator: "")
break
case MFMailComposeResultSaved.rawValue:
print("Email Saved as a Draft", terminator: "")
break
case MFMailComposeResultSent.rawValue:
print("Email Sent Successfully", terminator: "")
break
case MFMailComposeResultFailed.rawValue:
print("Email Send Failed", terminator: "")
break
default:
break
}
self.dismissViewControllerAnimated(true, completion: nil)
}
スキーム起動例
let url = NSURL(string: "schmehogehoge://")!
if UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.sharedApplication().openURL(url)
}
※urlスキームが起動できない場合(許可されていない、インストールされていないなど)は、canOpenURLでfalseが返ってきます。
アプリ側でスキームを許可する場合は、Info.plistで「LSApplicationQueriesSchemes」を作って設定させるようです。
こちらはまだ、未確認のため、こちらを参照ください。
【iOS9】特定のURLスキームのみを呼び出し可能にする→.canOpenURL(url)について
ibisメーラーとの連携もできることを確認
ibis よくある質問 – 質問と回答

コメント