ios - 从 iOS 应用程序发送的电子邮件

标签 ios swift email parse-platform

在我的应用中,用户可以通过点击按钮从应用中发送电子邮件。

所需函数从 parse.com 对象接收电子邮件收件人。

第一次调用该函数时,应用会抛出错误:不是有效的电子邮件地址。

我已将 print("Email=",self.emailConsulta) 用于检查收到的电子邮件地址是否有效,并且有效。

然后我关闭电子邮件,如果我再次点击发送电子邮件按钮,它会正常工作,并且设备中的电子邮件应用程序将收到的电子邮件地址显示为电子邮件的收件人。

这是代码,我还没有发现那里的问题,欢迎任何帮助:

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

func configuredMailComposeViewController() -> MFMailComposeViewController {

    let query = PFQuery(className: "datos_contacto")
    query.getObjectInBackgroundWithId("G5w8G3kVBG", block: {
        (questionObject: PFObject?, error: NSError?) -> Void in

        let direccion: AnyObject! = questionObject!.objectForKey("dato_contacto")

        self.emailConsulta  = direccion as! String

        print("Email=",self.emailConsulta)


    })

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


    print (emailConsulta)

    mailComposerVC.setToRecipients([emailConsulta])
    mailComposerVC.setSubject("Enviado desde Pedro Villarejo App Clientes (iOs)...")
    mailComposerVC.setMessageBody("Escriba aqui su texto", 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

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    controller.dismissViewControllerAnimated(true, completion: nil)

}

最佳答案

您正在后台从 Parse 请求目标邮件地址,因此在某些情况下,您的函数将在从 Parse 检索数据之前返回。您可以重组代码,以便从完成闭包中呈现邮件撰写 Controller -

@IBAction func sendEmailButtonTapped(sender: AnyObject) {
    if MFMailComposeViewController.canSendMail() {
        self.showMailComposeController()
    } else {
        self.showSendMailErrorAlert()
    }
}

func showMailComposeController() {

    let query = PFQuery(className: "datos_contacto")
    query.getObjectInBackgroundWithId("G5w8G3kVBG", block: {
        (questionObject: PFObject?, error: NSError?) -> Void in

        let direccion: AnyObject! = questionObject!.objectForKey("dato_contacto")

        self.emailConsulta  = direccion as! String

        print("Email=",self.emailConsulta)

        let mailComposerVC = MFMailComposeViewController()

        mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property

        mailComposerVC.setToRecipients([emailConsulta])
        mailComposerVC.setSubject("Enviado desde Pedro Villarejo App Clientes (iOs)...")
        mailComposerVC.setMessageBody("Escriba aqui su texto", isHTML: false)
        self.presentViewController(mailComposeViewController, animated: true, completion: nil) 

    })

}

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

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    controller.dismissViewControllerAnimated(true, completion: nil)

}

关于ios - 从 iOS 应用程序发送的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35514312/

相关文章:

ios - 通过 TableView ViewForSectionHeader 打洞并访问下面的 View

java - 无法在spring MVC中使用jboss 5.0运行时服务器的mail-service.xml发送邮件

android - "java.lang.SecurityException: Permission Denial: starting Intent"在 Android 上发送电子邮件

java - 在 mimemessage 中添加 "addinline"函数时出现 NullPointerException

ios - Firebase Analytics 不可用

ios - 如何通过 swift 在 iCarousel 库中使用 .Wrap 选项?

iOS:OneSignal PresentAppSettings() 不存在

iphone - iOS - 我很困惑这里是如何处理内存的?

ios - UITableViewController 中的死区

iOS Metal 无法执行 'metal'(没有这样的文件或目录)