【问题标题】:Send email from an App directly - Swift直接从应用程序发送电子邮件 - Swift
【发布时间】:2017-08-01 15:43:43
【问题描述】:

我想从我的应用程序中发送邮件。我正在使用 Swift 迈出第一步,但我一直坚持下去。我想按一个按钮并直接发送电子邮件。我想从我的 TextField 中获取 Subject 和 MessageBody 的数据。包括来自我的 TextField 的发件人数据(请查看屏幕截图) 怎么做? 这是我的代码:

@IBAction func sendEmailButtonTapped(sender: AnyObject) {
    let mailComposeViewController = configuredMailComposeViewController()
    if MFMailComposeViewController.canSendMail() {
        self.present(mailComposeViewController, animated: true, completion: nil)
    } else {
        self.showSendMailErrorAlert()
    }
}

func configuredMailComposeViewController() -> MFMailComposeViewController {
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property

    mailComposerVC.setToRecipients(["primer@gmail.com"])
    mailComposerVC.setSubject("Sending you an in-app e-mail...")
    mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)

    return mailComposerVC
}

func showSendMailErrorAlert() {
    let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail.  Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
    sendMailErrorAlert.show()
}

// MARK: MFMailComposeViewControllerDelegate Method
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    controller.dismiss(animated: true, completion: nil)
}

【问题讨论】:

  • 您的第一步不包括运行搜索吗?截图在哪里?你的问题是什么?
  • @ElTomato 搜索什么?我添加了一个问题
  • 类似主题...您不是第一个提出这个问题的人。甚至不是第十...
  • @ElTomato 和?谢谢你的信息))

标签: swift email button action textfield


【解决方案1】:

你是说你想在幕后发送电子邮件,而不让用户看到电子邮件视图控制器出现?如果这就是你想要的,你不能从你的(前端)代码中做到这一点。 Apple 不希望应用程序能够以自动方式发送电子邮件,而无需用户自己点击发送按钮。如果您愿意,您可以编写从应用服务器端发送电子邮件的功能。

【讨论】:

  • 我希望用户在按下 SEND 按钮后仍留在应用程序中
  • 当电子邮件视图控制器出现时,用户仍在您的应用程序中,尽管电子邮件视图控制器看起来与您 iPhone 附带的电子邮件应用程序一模一样。
  • 我可以从我的 TextField 中获取 Subject 和 MessageBody 的数据用于电子邮件吗?
  • 是的。而不是mailComposerVC.setSubject("Sending you an in-app e-mail..."),请执行以下操作:mailComposerVC.setSubject(subjectTextField.text),如果 subjectTextField 是用户键入主题的文本字段的名称。然后对消息体做同样的事情。
猜你喜欢
  • 2016-10-18
  • 1970-01-01
  • 1970-01-01
  • 2017-07-03
  • 2013-04-13
  • 2021-06-08
  • 2020-05-24
  • 2011-05-08
  • 2011-08-06
相关资源
最近更新 更多