2

Im using MFMailComposeViewController to send emails, but after you sent an email I would like to show the email and time it was done on a tableview. How can I do that?

My table code is basic

//This is empty, just to populate the empty table for now
var sentEmails = [String]()

//Number of Sections
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

//Number of Rows
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sentEmails.count
}

//Cell Configuration
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)

    let row = indexPath.row
    cell.textLabel?.text = sentEmails[row] //Email here?
    cell.detailTextLabel?.text = "time stamp"//maybe?

    return cell
}

This is my code for the mail

 func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {

    //Checks if composer can sent email
    if MFMailComposeViewController.canSendMail() {

        let mail = MFMailComposeViewController()

        mail.mailComposeDelegate = self

        //Add the email to the recipient field
        if let emailAddress = contactProperty.value as? String {
            mail.setToRecipients([emailAddress])
        }

        //Dismisses the current view and presents the mail composer view
        self.dismissViewControllerAnimated(true, completion: {() -> Void in
            self.presentViewController(mail, animated: true, completion: nil)
        })

    } else {
        print("send error")
    }
}

//Dismiss Buttons for Mail Composer
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError?) {

    switch result {
    case MFMailComposeResultCancelled:
        print("Mail Cancelled")
    case MFMailComposeResultSaved:
        print("Mail Saved")
    case MFMailComposeResultSent:
        print("Mail Sent")
    case MFMailComposeResultFailed:
        print("Mail sent failure: \(error)")
    default:
        break
    }

    controller.dismissViewControllerAnimated(true, completion: nil)
}

How can I get the sent email and time to show on my table? Thank you in advance.

Nyxx
  • 303
  • 3
  • 13

1 Answers1

0

MFMailComposeViewController runs out of process, and by design does not provide any more information except for completion status.

If you want more information, you'll need to implement sending emails manually which might be a very tedious task I'd strongly advise against unless this is a core of your product.

Update You can not get it using built in mail composer. If you prefer to reimplement email client, there are several open source clients: http://libmailcore.com; (keep in mind that you won't have access to user accounts forcing her to set up account from scratch). Alternatively you can implement sending email from your server or 3rd party service providing only UI on the client, e.g. 3rd party services https://github.com/sendgrid/sendgrid-objc

Sash Zats
  • 5,376
  • 2
  • 28
  • 42
  • So how would I get the time and emails I sent? I only needed this information and nothing else. I'm new to swift so I'm not very familiar how alot of it works. – Nyxx Aug 23 '16 at 22:03
  • Thank for the update. You just gave me an idea. In the completion area I can just send the email i just used and the current NSDate and just sent that to an array. Not sure how to do it, but sounds like a good idea. – Nyxx Aug 23 '16 at 23:04