【问题标题】:button from app opens email but won't close the window and return to app应用程序中的按钮打开电子邮件但不会关闭窗口并返回应用程序
【发布时间】:2015-12-15 23:50:14
【问题描述】:

我的应用程序中有一个按钮,可以打开一封要发送给我的电子邮件,当按下此按钮时,iPhone 上的电子邮件应用程序会打开,当按下发送时,电子邮件会发送,但窗口不会关闭并且然后返回我的应用程序。此外,当我按下取消时,它会提供保存/删除草稿的选项,但不会再次关闭窗口并返回我的应用程序。我在下面附上了电子邮件代码。

@IBAction func SendMessage(sender: AnyObject) {

var mail: MFMailComposeViewController!

let toRecipients = ["usalim76@gmail.com"]
let subject = "Enquiry"
let body = "Your body text"

mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(toRecipients)
mail.setSubject(subject)
mail.setMessageBody(body, isHTML: true)

presentViewController(mail, animated: true, completion: nil)

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


}

【问题讨论】:

    标签: ios swift email mfmailcomposeviewcontroller


    【解决方案1】:

    你好像忘了实现MFMailComposeViewControllerDelegate,添加这个:

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

    【讨论】:

    • 仍然有同样的问题,窗口没有关闭并返回我的应用程序,我已经编辑了上面的代码
    【解决方案2】:

    设法修复它!

         @IBAction func SendMessage(sender: AnyObject) {
    
        let mailComposeViewController = configuredMailComposeViewController()
        if MFMailComposeViewController.canSendMail() {
            self.presentViewController(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(["someone@somewhere.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!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-07
      • 2011-11-03
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多