通过 UIAlertView 按钮打开 iOS 电子邮件应用程序

Open iOS Email app through UIAlertView button

我有一个通过 UIButton 触发的 UIAlertView。

UIAlertView 显示两个按钮,"Open Email" 和 "Cancel"。

"Cancel" 从视图中删除 UIAlert。我试图做到这一点,当用户点击 "Open Email" 时,他们的设备会打开默认电子邮件应用程序的撰写屏幕,并且电子邮件地址已经在 "sender" 部分中。

使用 Swift 3.

谢谢!

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.
}
*/

}

我用这段代码得到了它:

    @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: { (actionSheetController) -> Void in let email = "email@example.com"
        let url = NSURL(string: "mailto:\("email@example.com")")
        UIApplication.shared.openURL(url as! URL)})


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


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



}

您必须使用MFMailComposer发送电子邮件,这就是您的使用方式

if MFMailComposeViewController.canSendMail() {
        let mail = MFMailComposeViewController()
        mail.mailComposeDelegate = self
        mail.setToRecipients(["rajatpagare@hotmail.com"])
        mail.setMessageBody("you email body", isHTML: false)
        present(mail, animated: true)
    } else {
        // email is not added in app
    }

也是import MessageUI框架并符合MFMailComposeViewControllerDelegate

此外,当您将用户从您的应用程序重定向到另一个应用程序时,您不需要像您在回答中提到的那样使用 openURL,您可以使用 MFMailComposer.

首先,我假设您需要在您的代码片段中添加的只是这一部分:

@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)
}

无论如何,你需要的是在创建UIAlertAction实例时填充handler。参考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.

所以,你的 openEmail 应该是这样的:

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

        })

我不太确定您想如何发送电子邮件的机制,但我认为您可能想要检查一下 MFMailComposeViewController, this question 应该可以帮助您使用它。

希望对您有所帮助。