在Swift中,MFMailComposeViewController无法将变量读取到消息中。

问题描述 投票:0回答:1

我有一个小问题。我使用MFMailComposeViewController在下班后发送邮件。所以我有一个var allRuns,我在一些地方打印了这个var,它总是显示值=3,但是在我的MailViewController的MessageBody中,它等于0nil(我设置了0)。如果这很重要的话--我是从Firebase中获取的var,但在应用程序中更早。我应该在哪里寻找问题?这是下面的一些代码。

谢谢,注意安全

MailViewController:

import UIKit
import MessageUI

class MailViewController: UIViewController, MFMailComposeViewControllerDelegate {

let av = ActualValues()

override func viewDidLoad() {
    super.viewDidLoad()

}

//MARK: - Buttons

@IBAction func emailButtonTapped(_ sender: UIButton) {
    showMailComposer()
}

@IBAction func backButtonTapped(_ sender: UIButton) {
    self.dismiss(animated: true, completion: nil)
}


//MARK: -Func

func showMailComposer() {
    if MFMailComposeViewController.canSendMail() {
        let mailComposer = MFMailComposeViewController()
        mailComposer.mailComposeDelegate = self
        mailComposer.setToRecipients(["[email protected]"])
        mailComposer.setSubject("Work of \(av.currentDate())")
        mailComposer.setMessageBody("All runs: \(av.allRuns)", isHTML: false)

        present(mailComposer, animated: true, completion: nil)
    }
}

func mailComposeController(_ controller: MFMailComposeViewController,
                           didFinishWith result: MFMailComposeResult, error: Error?) {
    if let _ = error {
        //show alert
        controller.dismiss(animated: true, completion: nil)
        return
    }

    switch result {
    case .cancelled:
        print("Cancelled")
    case .failed:
        print("Failed")
    case .saved:
        print("saved")
    case .sent:
        print("Email sent")
    @unknown default:
        print("default")
    }
    controller.dismiss(animated: true, completion: nil)

}

}

av = ActualValuse中的几行。

var allRuns: Int = 0
var runs: Array? = []

//All runs
func allRunsFunc() {
    let ref1 = ref.child("\(currentYear())/\(currentMonth())/\(currentDay())")
    ref1.observeSingleEvent(of: .value) { (snapshot) in
        if let snapshots = snapshot.children.allObjects as? [DataSnapshot] {
            self.allRuns = snapshots.count as Int
        }
    }
}

希望我上传了你需要的一切

EDIT.我想说的是,我应该在MFMAilComposeViewController之前使用segue吗?我应该在MFMAilComposeViewController之前使用segue吗?如果是,我应该如何配置它?

import UIKit
import MessageUI

class MailViewController: UIViewController, MFMailComposeViewControllerDelegate {

let av = ActualValues()


override func viewDidLoad() {
    super.viewDidLoad()

}

//MARK: - Buttons

@IBAction func emailButtonTapped(_ sender: UIButton) {
    showMailComposer()
}

@IBAction func backButtonTapped(_ sender: UIButton) {
    self.dismiss(animated: true, completion: nil)
}


//MARK: -Func


func showMailComposer() {
    if MFMailComposeViewController.canSendMail() {
        let mailComposer = MFMailComposeViewController()
        mailComposer.mailComposeDelegate = self
        mailComposer.setToRecipients(["[email protected]"])
        mailComposer.setSubject("Work of \(av.currentDate())")
        mailComposer.setMessageBody("\(av.exportText())", isHTML: true)

        present(mailComposer, animated: true, completion: nil)
    }
}

func mailComposeController(_ controller: MFMailComposeViewController,
                           didFinishWith result: MFMailComposeResult, error: Error?) {
    if let _ = error {
        //show alert
        controller.dismiss(animated: true, completion: nil)
        return
    }

    switch result {
    case .cancelled:
        print("Cancelled")
    case .failed:
        print("Failed")
    case .saved:
        print("saved")
    case .sent:
        print("Email sent")
    @unknown default:
        print("default")
    }
    controller.dismiss(animated: true, completion: nil)

}

func configureMail(model: ActualValues) {
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.destination is MFMailComposeViewController {
    let vc = segue.destination as? MFMailComposeViewController
        vc?.configureMail(model: av)
    }
}
}
ios swift firebase mfmailcomposeviewcontroller
1个回答
0
投票

你似乎错过了执行 allRunsFunc 函数。也许在ActualValue init 方法。

© www.soinside.com 2019 - 2024. All rights reserved.