单击表格视图单元格时启动默认电子邮件应用程序 - IOS

Posted

技术标签:

【中文标题】单击表格视图单元格时启动默认电子邮件应用程序 - IOS【英文标题】:Launch Default Email App when clicked on Table View Cell - IOS 【发布时间】:2018-07-16 00:18:42 【问题描述】:

我有这个标签,点击该标签后,用户应该会转到他们的默认电子邮件应用程序并为他们撰写邮件。

有人可以告诉我如何实现这个吗?

Let's say when someone clicks on feedback, they should be redirected to their default email app

【问题讨论】:

如果你想实现邮件功能,那么试试 MFMailComposeViewController。哪个推送邮件控制器,您可以在其中发送邮件。 【参考方案1】:

这是发送邮件的代码。请阅读cmets了解代码

  func sendEmail() 
  // check if device can send mail
    if MFMailComposeViewController.canSendMail() 
 // create MFMailComposeViewController object
        let mail = MFMailComposeViewController()
// set delegate to know when email has been sent and perform further tasks
        mail.mailComposeDelegate = self
// In setToRecipients you can pass array of email to whom you want to send email
        mail.setToRecipients(["someemail.com"])
// In setMessageBody you can pass the email body text
        mail.setMessageBody("<p>some email text</p>", ishtml: true)
        // here you can present the controller
        present(mail, animated: true)
     else 
        // show failure alert
        //Unable to send Email(email may not be configured in your device)
        // or may be you are using simulator
    

现在您可以使用委托方法来关闭 MFMailComposeViewController

 extension YourViewController : MFMessageComposeViewControllerDelegate

func messageComposeViewController(_ controller: MFMessageComposeViewController,
                                  didFinishWith result: MessageComposeResult) 
    // use result for further task
    // Dismiss the MFMailComposeViewController.
    controller.dismiss(animated: true, completion: nil)

 

【讨论】:

【参考方案2】:

以下是在 swift 4 上呈现默认电子邮件应用程序的扩展。

import MessageUI 
extension UIViewController : MFMailComposeViewControllerDelegate 

func configuredMailComposeViewController(recipients : [String]?, subject :
    String, body : String, isHtml : Bool = false,
            images : [UIImage]?) -> MFMailComposeViewController 
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self // IMPORTANT

    mailComposerVC.setToRecipients(recipients)
    mailComposerVC.setSubject(subject)
    mailComposerVC.setMessageBody(body, isHTML: isHtml)
    //If you want to send images on Mail
    for img in images ?? [] 
        if let jpegData = UIImageJPEGRepresentation(img, 1.0) 
            mailComposerVC.addAttachmentData(jpegData,
                                             mimeType: "image/jpg",
                                             fileName: "Image")
        
    

    return mailComposerVC


func presentMailComposeViewController(mailComposeViewController :
    MFMailComposeViewController) 
    if MFMailComposeViewController.canSendMail() 
        self.present(mailComposeViewController,
                                   animated: true, completion: nil)
     else 
       self.showErrorAlert()
    


public func mailComposeController(controller: MFMailComposeViewController,
                                  didFinishWith result: MFMailComposeResult,
                                  error: Swift.Error?) 
    switch (result) 
    case .cancelled:
        self.dismiss(animated: true, completion: nil)
    case .sent:
        self.dismiss(animated: true, completion: nil)
    case .failed:
        self.dismiss(animated: true, completion: 
       self.showErrorAlert()

        )
    default:
        break;
    


func showErrorAlert() 
     let sendMailErrorAlert = UIAlertController.init(title: "Failed",
                                                            message: "Unable to send email. Please check your email " +
                "settings and try again.", preferredStyle: .alert)
            sendMailErrorAlert.addAction(UIAlertAction.init(title: "OK",
                                                            style: .default, handler: nil))
            self.present(sendMailErrorAlert,
                                       animated: true, completion: nil)


现在你可以像这样调用这些方法了。

@IBAction func didTapOpenEmailApp(sender: Any) 
    let toRecipients = ["desigredMail@example.com"]
    let subject = "Your subject here"
    let body = "Enter comments here..."
    let mail = configuredMailComposeViewController(recipients: toRecipients, subject: subject, body: body, isHtml: false, images: nil)
    presentMailComposeViewController(mailComposeViewController: mail)

编码愉快!

【讨论】:

【参考方案3】:
// email 
 let address = "test@test.com"
 let url = URL(string: "mailto://\(address)")
 UIApplication.shared.openURL(url!)

【讨论】:

以上是关于单击表格视图单元格时启动默认电子邮件应用程序 - IOS的主要内容,如果未能解决你的问题,请参考以下文章

滚动单元格时表格视图设置消失

表格视图在按钮单击时更新单元格时反弹

在单击表格单元格时推送新视图,这怎么可能? [关闭]

单击表格单元格时将变量值发送到下一个视图控制器

单击表格单元格时将 UIViewController 滚动到顶部

仅当在表格视图中选择单元格时,详细信息披露才会做出反应