Swift send email from your iOS 9 app

Swift send email from your iOS app

Swift send email from your iOS app. How do we do that…
It was easier than I first thought. At least with this method. Using the MessageUI and have the app to pre-fill an email and using iOS mail to send it.

First we need to import the MessageUI. We will do that from the view controller in my example code. By the way you can download the example code from my github. Just go to https://github.com/solron/Swift-Send-Email and download the code.

The we will need to setup the MFMailComposeViewControllerDelegate.

// Set delegate
class MyViewController: UIViewController,MFMailComposeViewControllerDelegate {
// Code goes here
}

My swift send email example only contains a send button on the user interface. Not showing here how to link an action to a button. But inside the button action code we do something like this:

@IBAction func btnSendEmail(sender: AnyObject) {
let email = MFMailComposeViewController()
email.mailComposeDelegate = self
email.setSubject("Subject goes here")
email.setMessageBody("Some example text", isHTML: false)
email.setToRecipients(["post@email.com"]) // the recipient email address
if MFMailComposeViewController.canSendMail() {
presentViewController(email, animated: true, completion: nil)
}
}

The code above will populate all fields needed to send you email. Subject field, recipient address and message body. You can of course edit any of these in the email that pops up before you send it. Because you will need to confirm the mail. Just like any other email you send. Just that you dont have to enter the information your self. The fields doesn’t have to be fixed in your code either. You can have dynamic values changed programatically based on anything you want.

Last we will need to enter the function in order for the delegate to work. And that is something like this:
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
dismissViewControllerAnimated(true, completion: nil)
}

Now you have all the code you need to send emails from your app.

Thats it for swift send email.

Happy emailing!

Related Posts

Xcode 9 iOS icon sizes

Xcode 9 iOS icon sizes overview

Xcode 9 iOS icon sizes overview Xcode 9 iOS icon sizes overview. They keep adding more and more icon sizes. Xcode 9 has the most icon sizes…

Xcode 9 uiimage image not shown

Xcode 9 uiimage image not shown

Xcode 9 uiimage image not shown Working on some ios apps created with Xcode 8 and your images don’t show after upgrading to Xcode 9? Took me awhile…

Xcode 8 iOS icon sizes overview

Xcode 8 iOS icon sizes overview Xcode 8 iOS icon sizes overview. There are even more icon sizes since Xcode 7. And it can be an extra challenging…

Swift Capitalize

Swift Capitalize words, How to

Swift Capitalize, How to Swift Capitalize is done with a string instance property. Check the example below to see how to use it. There is also a link…

Swift Debian

Swift Debian 8 (Jessie)

Swift Debian 8 (Jessie) Swift, Apples new programming language, to take over for Objective C has been open source for some time now. It only took at…

didMove never get called

Xcode – didMove never get called

didMove never get called in Xcode? didMove never get called when creating a new project with Xcode using the Game template. I selected Swift and SpriteKit. I…

This Post Has 2 Comments

Leave a Reply