Add recipient in MFMailComposeViewController from Label represented - ios

I have an application that allows users to send emails by MFMailcomposeViewController.
composeVC.setToRecipients(["email#gmail.com"])
In the same view controller i have a label which is representing the email of the people which is downloaded from Firestore. How can i setToRecipients instead of (["email#gmail.com"]) to (["mailRepresentedLabel#gmail.com"])
I want it to pull the data from mail represented label and add it automatically to the recipient so the end user is not required to add the email to setToRecipient it will be automatically pulled from mailRepresentedLabel
Please help.
My current code is look like this
if !MFMailComposeViewController.canSendMail() {
print("Не удается отправить Имэйл")
return
}
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["\(String(describing: mailRepLabel))"])
composeVC.setSubject("Register your client details with us")
composeVC.setMessageBody("Dear agent please register your client with us by replying on that email in order for us to track the information that this client is came with you. if aftersometime the client would like to come without you we will always have the information that this client is came with you and we will send him back to you. Please reply with the following details: Client Name, Passport number Property Managers name.", isHTML: false)
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)
print("done")
}
}
extension AgentViewController: MFMailComposeViewControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
if let _ = error {
controller.dismiss(animated: true)
return
}
switch result {
case .cancelled:
print("Canceled")
case.failed:
print("Failed to send")
case.saved:
print("Saved")
case.sent:
print("Email Sent")
}
controller.dismiss(animated: true)
}
}```

You can access the text content of your label with the text attribute. So... Something like this?
mailComposer.setToRecipients([mailRepLabel.text])

Related

How can I avoid an 1100 error using MFMailComposeViewControllerDelegate?

I'm trying to send an email from with my Swift app. Nothing fancy. Subject. Body. Recipients. I'm doing this like the examples I've seen which is to say NOT using a separate view controller - just a basic vc but specifying MFMailComposeViewControllerDelegate like so:
class MyVC: UIViewController, MFMailComposeViewControllerDelegate
I then have the two methods I see in all the examples:
#IBAction func sendEmail()
{
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setSubject(subject)
mail.setToRecipients(recipients)
mail.setMessageBody(content, isHTML: true)
present(mail, animated: true)
} else {
// show failure alert
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
{
controller.dismiss(animated: true)
}
But when the email gets cancelled (Delete Draft) I get:
[AXRuntimeCommon] AX Lookup problem - errorCode:1100 error:Permission denied portName:'com.apple.iphone.axserver'
No crashing. Emails get sent just fine. But I noticed that the cancelled emails pile up in Mail. The drafts are not being deleted. What am I missing? Does anyone have this kind of thing working?

iOS Swift 3 present MFMailComposeViewController leads to crash

Hey I am using this code to send an email from within my app
#IBAction func sendEmail(_ sender: Any) {
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["test#gmail.com"])
composeVC.setSubject("Feedback")
composeVC.setMessageBody("", isHTML: false)
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil) //CRASHES
}
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult, error: Error?) {
// Check the result or perform other tasks.
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
}
The app is live and emails from users do reach me. Often they are empty except for "Sent from my iPhone" but I don't think that's a programming issue. What is an issue tho is that apparently, last week, 8 devices dropped a crash report because of the "//CRASHES" line.
I think the simulator crashes when trying to do this but I haven't used the simulator for this installation for weeks so those crashreports on iTunesConnect are legit crashes.
What happens if the user doesn't have the email app installed?
You can verify that the user has setup the device for sending mail by calling
[MFMailComposeViewController canSendMail]
before performing any actions
According to Apple Documentation MFMailComposeViewController
Before presenting the mail compose view controller, always call the the canSendMail() method to see if the current device is configured to send email. If the user’s device is not set up for the delivery of email, you can notify the user or simply disable the email dispatch features in your application. You should not attempt to use this interface if the canSendMail() method returns false.
So, initialization of mail compose view controller using MFMailComposeViewController() returns nil, if the user’s device is not set up for the delivery of email and app is terminating due to present a nil modal view controller.
Therefore, before presenting the mail compose view controller you should alway check device's mail settings.
So try this lines of code
#IBAction func sendEmail(_ sender: Any) {
if !MFMailComposeViewController.canSendMail() {
print("Mail services are not available")
return
}
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["test#gmail.com"])
composeVC.setSubject("Feedback")
composeVC.setMessageBody("", isHTML: false)
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil) //CRASHES
}

Even though app seems to be working fine i did not receive any email

I made a camera app that takes a pic and once a picture is taken i tap on a uibutton to compose a mail with that image as attachment. everything seems to be working fine even the image file in attachment can be seen while composing the email. when tap on 'send', and open my email id I do not receive any new inbox. here is the code
func sendEmail() {
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients([email])
composeVC.setSubject("Hello!")
composeVC.setMessageBody("Hello this is my message body!", isHTML: false)
let imageData: NSData = UIImagePNGRepresentation(pickedImage.image!)! as NSData
composeVC.addAttachmentData(imageData as Data, mimeType: "image/jpeg", fileName: name)
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
I found the issue & it was because I didn't allow the mail app on my iOS device access to "Mobile Data". So, don't forget to allow that access as your device will be sending mail via the apple id you logged in with.

How to close MFMailComposeViewController?

I want to send an email from my Swift application and it works well, it sends the email.
Nevertheless, after sending the email, the layout does not quits. I want to quit the layout after sending the email. I also want this behaviour when I press on Cancel, Remove draft or Save draft button.
This is the code that I have to send the email (after pressing a button):
#IBAction func btnSendEmailAction(_ sender: AnyObject) {
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
composeVC.setToRecipients(["mymail#mail.com"])
composeVC.setSubject("Hello!")
composeVC.setMessageBody("Hello World!", isHTML: false)
self.present(composeVC, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
switch result {
case MFMailComposeResult.cancelled:
controller.dismiss(animated: true, completion: nil)
break
case MFMailComposeResult.sent:
controller.dismiss(animated: true, completion: nil)
break
case MFMailComposeResult.failed:
controller.dismiss(animated: true, completion: nil)
break
default:
break
}
controller.dismiss(animated: true, completion: nil)
}
but the layout does not quits when I press on Cancel or Sent buttons.
I know that there are a lot of questions related about this problem but I looked a lot of them and this is the code I could get from a mix of some of them. Note that most of them are in Objective instead of Swift (and sometimes the methods does not exist).
Example: iPhone: How to Close MFMailComposeViewController?
Am I missing something on my code? How can I detect Remove draft and Save Draft events?
Thanks in advance!
Looks like you're using swift 3 and not using a valid delegate method. Corrected delegate method is this:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
switch result {
case .cancelled:
break
case .saved:
break
case .sent:
break
case .failed:
break
}
dismiss(animated: true, completion: nil)
}

How to send email in app from UI Label (Swift 2 IOS 9)?

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!!
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;
}
}

Resources