MFMailComposeViewController setToRecipients append String from settings textField trouble

冷暖自知 提交于 2019-12-13 04:33:48

问题


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
}


来源:https://stackoverflow.com/questions/37451874/mfmailcomposeviewcontroller-settorecipients-append-string-from-settings-textfiel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!