Skip to content

Instantly share code, notes, and snippets.

@tsraveling
Last active August 17, 2017 06:16
Show Gist options
  • Save tsraveling/c05a8a11a086dd7947b2372211779bb6 to your computer and use it in GitHub Desktop.
Save tsraveling/c05a8a11a086dd7947b2372211779bb6 to your computer and use it in GitHub Desktop.
Email
import UIKit
import MessageUI
extension UIViewController : MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate, UINavigationControllerDelegate {
func sendEmail(_ to: [String], defaultSubject: String, defaultBody: String, _ completion : ((_ result: MFMailComposeResult) -> Void)? = nil) {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(to)
mail.setMessageBody(defaultBody, isHTML: true)
mail.setSubject(defaultSubject)
present(mail, animated: true)
} else {
self.alert("Mail is not available on this device.")
}
}
func sendText(_ to: [String], defaultBody: String, _ completion : ((_ result: MessageComposeResult) -> Void)? = nil) {
guard MFMessageComposeViewController.canSendText() else {
self.alert("SMS is not available on this device.")
return
}
let controller = MFMessageComposeViewController()
controller.body = "Message Body"
controller.recipients = to
controller.delegate = self
controller.messageComposeDelegate = self
self.present(controller, animated: true, completion: nil)
}
// MARK: - Mail Delegate -
public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
// MARK: - Message Delegate -
public func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
//... handle sms screen actions
controller.dismiss(animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment