MailController is a lightweight Swift wrapper around MFMailComposeViewController, which provides you with a convenient interface.
It eliminates the use of delegation pattern and instead provides a block-based API if necessary.
MFMailComposeViewController has issues by reusing, that's why it's not encouraged to do so.
Instead MailController provides you with a singleton and takes care about releasing of MFMailComposeViewController after dismissing it.
To run the example project, clone the repo, and run pod install
from the Example directory first.
- iOS 9.0+
MailController is available through CocoaPods.
To integrate it into your Xcode project using CocoaPods, specify it in your Podfile
:
platform :ios, '9.0'
target '<Your Target Name>' do
pod 'MailController', '~> 1.0'
end
Alternatively you can directly add the Mailcontroller.swift
(and optionaly MailControllerDefaultResultHandler.swift
) source file to your project.
import MailController
class ViewController: UIViewController {
@IBAction func mailAction() {
if let mailComposeViewController = MailController.shared.mailComposeViewController() {
mailComposeViewController.setToRecipients(["email@example.com"])
mailComposeViewController.setSubject("Test")
mailComposeViewController.setMessageBody("Hello world!", isHTML: false)
present(mailComposeViewController, animated:true, completion:nil)
}
}
}
By default when the user has finished with the interface it will be dismissed.
But you can customize this behaviour and also make use of result and error parameters using completionHandler block:
@IBAction func mailAction() {
let mailComposeViewController = MailController.shared.mailComposeViewController { (controller, result, error) in
switch (result) {
case MFMailComposeResult.failed:
print("Failed to send Email with error: \(error?.localizedDescription ?? "")!")
default:
break
}
controller.dismiss(animated: true, completion: nil)
}
if let mailComposeViewController = mailComposeViewController {
present(mailComposeViewController, animated:true, completion:nil)
}
}
}
MailController is available under the MIT license. See the LICENSE file for more info.