1

Simply trying to allow the user to send emails. At the moment when the button "contact" is clicked, it takes me to a black screen rather than displaying the mailComposers.

The debugger responds with

2018-05-14 11:10:59.465952-0400 App[2333:757177] Failed to set (keyPath) user defined inspected property on (UIButton): [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath.

However, that only occurs when sliding the menu from left to right using the SWReveal function. When removing the code from below, all the other functionalities work properly. Its only when using the code from below that gives me the black screen at the moment "button contact" is pressed.

import Foundation
import UIKit
import MessageUI

class SendEmailVC: MFMailComposeViewController, MFMailComposeViewControllerDelegate
{
@IBAction func Send_Tapped(_ sender: Any)
{

    if MFMailComposeViewController.canSendMail()
    {
        contact()
        let mailComposeViewController = configureMailController() //FIXED √
        self.present(mailComposeViewController, animated: true, completion: nil)
    }
    else
    {
        showMailError()
    }
}

func configureMailController() -> MFMailComposeViewController
{
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self

    mailComposerVC.setToRecipients(["testing@gmail.com"])
    mailComposerVC.setSubject("Hello")
    mailComposerVC.setMessageBody("How are you doing?", isHTML: false)

    return mailComposerVC
}


/*
 * DON'T EDIT THE CODE BELOW.
 */


func showMailError()
{
    let sendMailErrorAlert = UIAlertController(title: "Email failed to send", message: "Your device fail to send the email", preferredStyle: .alert)

    let dismiss = UIAlertAction(title: "Dale", style: .default, handler: nil)
    sendMailErrorAlert.addAction(dismiss)
    self.present(sendMailErrorAlert, animated: true, completion: nil)
}

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
{
    controller.dismiss(animated: true, completion: nil)
}

}

Palermox
  • 63
  • 10
  • FYI - You need to call `configureMailController` after checking `canSendMail`. – rmaddy May 13 '18 at 05:01
  • 1
    Are there any messages in the debugger console when you run this code? Are you testing on a real iOS device? – rmaddy May 13 '18 at 05:02
  • rmaddy, where exactly would i call the configureMailController, I know you said after canSendMail, i get a warning saying "result of call _ is unused." The only message in the debugger that I am getting is 2018-05-13 00:09:04.648632-0400 TestingApp[1015:294288] Failed to set (keyPath) user defined inspected property on (UIButton): [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath. Only occurs when sliding the menu from left to right. I'm sure that can't be the problem, Also i am testing it on a real iOS device. – Palermox May 13 '18 at 05:12
  • Move the line `let mailComposeViewController = configureMailController()` inside the `if` statement. – rmaddy May 13 '18 at 05:15
  • Awesome, it removed the warning "result of call _ is unused." However, the black screen still remains. – Palermox May 13 '18 at 05:18

2 Answers2

3

Please use the code to open mail in swift with an extension like below.

extension SendEmailVC: MFMailComposeViewControllerDelegate {

func contact() {

    if !MFMailComposeViewController.canSendMail() {
        ROAlert.warningAlertUnderNavigation(ROConstants.More.kNoMailConfigured, onView: self.view)
        return ()
    }
    let composeVC = MFMailComposeViewController()
    composeVC.mailComposeDelegate = self
    composeVC.setToRecipients([ROConstants.More.kContactEmail])
    // Present the view controller modally.
    self.present(composeVC, animated: true, completion: nil)
}

//MARK: - MFMailComposeViewControllerDelegate

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)
}

}

saurabh
  • 6,687
  • 7
  • 42
  • 63
CrazyPro007
  • 1,006
  • 9
  • 15
  • Sorry I am kind of new to this, but I am still getting the black screen after implementing the extension similar to yours from above. – Palermox May 13 '18 at 16:51
  • 1
    @Palermox Can you please provide me the full implementation so that I can help you. – CrazyPro007 May 14 '18 at 03:13
0

Here is the new updated code that fixed the black screen problem. 1 step closer. But now I am getting the error message from showMailError() when pressing the send button. This is what the debugger displays

2018-05-14 15:03:40.474236-0400 projectName[2510:835559] [MC] Filtering mail sheet accounts for bundle ID: projectName, source account management: 1

import Foundation
import UIKit
import MessageUI

class SendEmailVC: UIViewController // MFMailComposeViewController: Caused black screen
{

@IBAction func SendButton_Tapped(_ sender: UIButton)
{

    if MFMailComposeViewController.canSendMail()
    {
        let mailComposeVC = self.configureMailController()
        self.present(mailComposeVC, animated: true, completion: nil)
    }
    else
    {
        self.showMailError()
    }
}

func configureMailController() -> MFMailComposeViewController
{
    let mailComposerVC = MFMailComposeViewController()

    mailComposerVC.setSubject("Hello")
    mailComposerVC.setMessageBody("How are you doing?", isHTML: true)
    mailComposerVC.setToRecipients(["<b>eddx544@gmail.com</b>"])
    mailComposerVC.mailComposeDelegate = self
    /*
     * mailComposerVC.addAttachmentData( attachment: date,
     mimeType: "String here",
     fileName:  "String here" )
     */
    return mailComposerVC
}


/*
 * DON'T EDIT THE CODE BELOW.
 */


func showMailError()
{
    let sendMailErrorAlert = UIAlertController(title: "Email failed to send", message: "Your device fail to send the email", preferredStyle: .alert)

    let dismiss = UIAlertAction(title: "Dismiss", style: .default, handler: nil)

    sendMailErrorAlert.addAction(dismiss)

    self.present(sendMailErrorAlert, animated: true, completion: nil)
}



}

extension SendEmailVC: MFMailComposeViewControllerDelegate
{
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
    {
        controller.dismiss(animated: true, completion: nil)
    }
}
Palermox
  • 63
  • 10