从iOS应用发送的电子邮件

时间:2016-02-19 20:23:10

标签: ios swift email parse-platform

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

所需的功能从parse.com对象接收电子邮件收件人。

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

我已经def row2tuple (row, md): method = row.__getitem__ if isinstance(row,list) else row.get return (method(md.first), method(md.next)) 检查收到的电子邮件地址是否有效,并且有效。

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

这是代码,我还没有在那里找到问题,欢迎任何帮助:

print("Email=",self.emailConsulta)

1 个答案:

答案 0 :(得分:1)

您正在后台从Parse请求目标邮件地址,因此在某些情况下,您的功能将在从Parse检索数据之前返回。您可以重新构建代码,以便从完成闭包中显示邮件撰写控制器 -

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

}