1

With manipulation of this tutorial http://www.raywenderlich.com/113772/uisearchcontroller-tutorial

I have a table view displaying people, when the cell is clicked the user is redirected to another view showing their picture and their email. I want to be able to have the user click on the email address and email them. I have researched and found similar tutorials https://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift/

The problem with the tutorial above is when test running the new code the ios simulator pops up an error and wont show the composed email (Maybe a Glitch?)and If the simulator did not give an error I dont know how to display multiple emails based on which person the user selected. Any help on solutions to this problem or any alternative will be great Thanks!!

0

1 Answer 1

1

The simulator will crash when you try to open the mail. Try it on an actual device instead.

To compose a mail do the following Add these to your class MFMessageComposeViewControllerDelegate and MFMailComposeViewControllerDelegate

In your didSelectRowAtIndexPath, this is the function called when you presses a row in your tableView. Do the following:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let candy = candies[indexPath.row] var mail: MFMailComposeViewController! // yourArray is the array that you use to populate the tableView // .mail is the variable in the object (I´m assuming you´re using objects in your array) let toRecipients = [candy[indexPath.row].email] let subject = "Feedback" let body = "<br><br><p>I have a \(UIDevice.currentDevice().modelName).<br> And iOS version \(UIDevice.currentDevice().systemVersion).<br</p>" mail = MFMailComposeViewController() mail.mailComposeDelegate = self mail.setToRecipients(toRecipients) mail.setSubject(subject) mail.setMessageBody(body, isHTML: true) presentViewController(mail, animated: true, completion: nil) } 

The delegate methods below if you need to use them

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { dismissViewControllerAnimated(true, completion: nil) } func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) { switch (result.rawValue) { case MessageComposeResultCancelled.rawValue: self.dismissViewControllerAnimated(true, completion: nil) case MessageComposeResultFailed.rawValue: self.dismissViewControllerAnimated(true, completion: nil) case MessageComposeResultSent.rawValue: self.dismissViewControllerAnimated(true, completion: nil) default: break; } } 
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the feedback, but the button is placed in a view controller
How would you know which email address the user has selected if the button is placed outside? Isn´t it better to just let the user click on the mail and then send the mail?
The button is inside the viewcontroller which is linked to a protoype cell. Depending on which populated cell the appropriate email will be listed. I followed the ray wenderlich tutorial if you want to see the basis how the app works. I manipulated his code by puting candies = [ Candy(category:"Chocolate", name:"Chocolate Bar", email:"[email protected]")
If I may suggest you, I would have done it as I wrote in my answer. In that way you will only have to select a row and do the mail code and set candies[indexPath.row].email as recipient and you will open a new mail with the recipient preselected.
Sorry I new to coding, could you provide some sample code?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.