面对MFMailComposerViewController的崩溃问题

时间:2015-05-07 11:15:07

标签: ios xcode swift

我在使用MFMailComposerViewController时遇到崩溃问题,这是我的代码:

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

崩溃信息:

特定应用信息: *由于未被捕获的异常终止应用' NSInvalidArgumentException',原因:' * - [NSRegularExpression enumerateMatchesInString:options:range:usingBlock:]: nil参数' 终止,未捕获类型

的异常
NSException abort()

称为

1 个答案:

答案 0 :(得分:0)

确认MFMailComposeViewControllerDelegate协议

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

//设置--mailComposeDelegate--属性非常重要,而不是--delegate--属性

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