MFMailComposeViewController setToRecipients append

2019-09-09 22:35发布

问题:

Hopefully this is a simple issue? But I just cant seem to get my head around it.

I have a text field in my settingsViewController called emailText where I want to set the default email recipient.

@IBOutlet weak var emailText: UITextField!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let shoppingListViewController = segue.destinationViewController as! ShoppingListViewController
    shoppingListViewController.emailString = ([emailText.text]!)
}

func textFieldShouldReturn(emailText: UITextField) -> Bool {
    emailText.resignFirstResponder()

    print ((emailText.text)!) //This prints the correct email address in the console.

    return true;
}

My shoppingListViewController recieves this string by the following:

var emailString = ""
mailComposerVC.setToRecipients ([emailString])

My problem is that when I press my button to open the email I get 2016-05-26 14:25:14.617 MY APP Name [1397:520893] is not a valid email address in the console? And a blank To: field in the opened email.

Where am I going wrong?

回答1:

//You have to confirm MFMailComposeViewControllerDelegate.


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

        mailComposerVC.setToRecipients([emailString])
        mailComposerVC.setSubject("Test 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)
    }


回答2:

Ok, so I'm still stuck for a fix. Ill lay out my issue again, in hopes that somebody will notice something not quite right with my segue.

I have a UITabBarController - It has 3 Tabs (1)Product List (2)Shopping List (3)Settings. So all three views also have UINavigationViewControllers. I have a Text Field in my Settings page to set the default recipient email address for MFMailComposeViewController (mailComposerVC.setToRecipients) which is launched by button press from shoppingListViewController. (Mail is working, I just don't get my to: address string populated) I can print to console the text field (emailText.text) just fine from settingsViewController. My segue does not carry all the way through to shoppingListViewController where it is required as a string.

SettingsVC (emailText) --> TabBarController --> NavController (2nd) --> ShoppingListVC (emailString = "emailText.text") --> mailComposerVC.setToRecipients([emailString])

My segue now looks like the following, but is still getting lost on the way.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let tabBarController = segue.destinationViewController as! UITabBarController
    let nav = tabBarController.viewControllers![2] as! UINavigationController
        let shoppingListViewController = nav.topViewController as? ShoppingListViewController
            shoppingListViewController!.emailString = emailText.text
}