Send Email using MFMailComposer (Template)
1.) Import MessageUI
import MessageUI
2.) Show Email
func showMail() { allow mailComposeViewController = configuredMailComposeViewController() if MFMailComposeViewController.canSendMail() { self.presentViewController(mailComposeViewController, animated: true, completion: nil) } else { self.showSendMailErrorAlert() } } func configuredMailComposeViewController() -> MFMailComposeViewController { allow mailComposerVC = MFMailComposeViewController() mailComposerVC.mailComposeDelegate = self // Extremely of import to laid upward the --mailComposeDelegate-- property, NOT the --delegate-- belongings mailComposerVC.setToRecipients([""]) mailComposerVC.setSubject("") mailComposerVC.setMessageBody("", isHTML: false) furnish mailComposerVC } func showSendMailErrorAlert() { allow sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could non ship e-mail. Please cheque email configuration in addition to stimulate again.", delegate: self, cancelButtonTitle: "OK") sendMailErrorAlert.show() } // MARK: MFMailComposeViewControllerDelegate Method func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) { controller.dismissViewControllerAnimated(true, completion: nil) switch result.value { example MFMailComposeResultCancelled.value: println("Mail cancelled") example MFMailComposeResultSaved.value: println("Mail saved") example MFMailComposeResultSent.value: println("Mail sent") showAlertMessage("Your post service has been sent successfully") example MFMailComposeResultFailed.value: println("Mail sent failure: \(error.localizedDescription)") showAlertMessage("Your post service has non been sent successfully") default: intermission } } func showAlertMessage(value : String){ allow alertController = UIAlertController(title: value, message: nil, preferredStyle: UIAlertControllerStyle.Alert) alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil)) self.presentViewController(alertController, animated: true, completion: nil) }
0 komentar:
Please comment if there are any that need to be asked.