0

I am getting crashing issue while working with MFMailComposerViewController here is my code:

let mailComposerView = MFMailComposeViewController()
    mailComposerView.setToRecipients(["abc@gmail.com"])
    mailComposerView.setSubject("XYZ")
    mailComposer.setMessageBody(
        "XYZ", isHTML: false)

Crashing Information:

Application Specific Information: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSRegularExpression enumerateMatchesInString:options:range:usingBlock:]: nil argument' terminating with un caught exception of type

NSException abort()

called

CMA
  • 1,528
  • 1
  • 11
  • 22
  • Can you add an exception breakpoint and see where it breaks? – Schemetrical May 07 '15 at 11:23
  • Can you add more explanation or code? I am not getting any error. – PK86 May 07 '15 at 11:40
  • **That's all and I have already mentioned crashing Information, I am just trying to open MFMailComposerViewController using these code, MFMailComposerViewController appears for few second and then crash with above mentioned issue ** – CMA May 07 '15 at 12:18
  • @Anya Refer this http://stackoverflow.com/questions/25604552/i-have-real-misunderstanding-with-mfmailcomposeviewcontroller-in-swift-ios8-in – Mrunal May 07 '15 at 13:17
  • **@Mrunal, Thanks I am working on simulator that might be the reason for this crashing issue** – CMA May 07 '15 at 13:27
  • Is there any way to add sender in mail like "From: Anya@gmail.com" – CMA May 07 '15 at 13:31

1 Answers1

0

Confirm MFMailComposeViewControllerDelegate protocol

class ViewController:UIViewController,MFMailComposeViewControllerDelegate 

{

override func viewDidLoad() 
{
    super.viewDidLoad()
}

@IBAction func sendEmailButtonTapped(sender: AnyObject) {
        let mailComposeViewController = configuredMailComposeViewController()
        if MFMailComposeViewController.canSendMail() {
            self.presentViewController(mailComposeViewController, animated: true, completion: nil)
        } else {
            self.showSendMailErrorAlert()
        }
}



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

// Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property

    mailComposerVC.setToRecipients(["someone@somewhere.com"])
    mailComposerVC.setSubject("Sending you an in-app e-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)
}
vijeesh
  • 1,317
  • 1
  • 17
  • 32