Miles Miles - 7 days ago 8
iOS Question

Open iOS Email app through UIAlertView button

I have a UIAlertView that is triggered via a UIButton.

The UIAlertView displays two buttons, "Open Email" and "Cancel".

"Cancel" removes the UIAlert from the view. I'm trying to make it so when the user taps "Open Email", their device opens the default email application's compose screen, with an email address already in the "sender" section.

Using Swift 3.

Thanks!

import UIKit
import Kingfisher

class SettingsViewController: UIViewController {

@IBAction func copyrightInfo(_ sender: Any) {


let alert = UIAlertController(title: "Copyright Info", message: "text", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "I understand", style: UIAlertActionStyle.default, handler: nil))

self.present(alert, animated: true, completion: nil)



}


@IBAction func helpfeedbackAlert(_ sender: Any) {

let alertController = UIAlertController(title: "Help & Feedback", message: "text", preferredStyle: .alert)

let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)

let openEmail = UIAlertAction(title: "Open Email", style: .default, handler: nil)


alertController.addAction(openEmail)
alertController.addAction(cancel)


self.present(alertController, animated: true, completion: nil)


}



@IBAction func clearCache(_ sender: Any) {


// SDImageCache.shared().clearMemory()
// SDImageCache.shared().clearDisk()

// Clear memory cache right away.
ImageCache.default.clearMemoryCache()

// Clear disk cache. This is an async operation.
ImageCache.default.clearDiskCache()


}

@IBAction func rateApp(_ sender: Any) {

if let url = URL(string: "https://www.google.com") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:]) {
boolean in
// do something with the boolean
}
} else {
// Fallback on earlier versions
}
}

}

@IBAction func purchasePhotos(_ sender: Any) {

if let url = URL(string: "https://google.com") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:]) {
boolean in
// do something with the boolean
}
} else {
// Fallback on earlier versions
}
}



}

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override var prefersStatusBarHidden: Bool {
get {
return true
}
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

Answer

First, I assume what do you need to add in your code snippet is only this part:

@IBAction func helpfeedbackAlert(_ sender: Any) {

    let alertController = UIAlertController(title: "Help & Feedback", message: "text", preferredStyle: .alert)

    let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)

    let openEmail = UIAlertAction(title: "Open Email", style: .default, handler: nil)


    alertController.addAction(openEmail)
    alertController.addAction(cancel)


    self.present(alertController, animated: true, completion: nil)
}

Anyway, what do you need is to fill the handler when you create an instance of the UIAlertAction. Referring to the documentation of the init(title:style:handler:):

handler

A block to execute when the user selects the action. This block has no return value and takes the selected action object as its only parameter.

So, your openEmail should be like:

let openEmail = UIAlertAction(title: "Open Email", style: .destructive, handler: { (actionSheetController) in
            // send your email here...

        })

I'm not pretty sure of the mechanism of how do you want to send an email, but I thik you might want to check MFMailComposeViewController, this question should help you to work with it.

Hope it helped.

Comments