Created
April 15, 2015 02:47
-
-
Save endodoug/67902c441535409386ec to your computer and use it in GitHub Desktop.
Swift Email - In app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import MessageUI | |
import Foundation | |
class ViewController: UIViewController, MFMailComposeViewControllerDelegate { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
@IBAction func sendEmailButtonTapped(sender: AnyObject) { | |
let mailComposeViewController = configuredMailComposeViewController() | |
if MFMailComposeViewController.canSendMail() { | |
self.presentViewController(mailComposeViewController, animated: true, completion: nil) | |
} else { | |
self.showSendMailErrorAlert() | |
} | |
} | |
func configuredMailComposeViewController() -> MFMailComposeViewController { | |
let mailComposerVC = MFMailComposeViewController() | |
mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property | |
mailComposerVC.setToRecipients(["endodoug@gmail.com"]) | |
mailComposerVC.setSubject("Sending you an in-app e-mail...") | |
mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false) | |
return mailComposerVC | |
} | |
func showSendMailErrorAlert() { | |
let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK") | |
sendMailErrorAlert.show() | |
} | |
// MARK: MFMailComposeViewControllerDelegate Method | |
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) { | |
controller.dismissViewControllerAnimated(true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment