ios - swift 错误 : "MailCompositionService quit unexpectedly"

标签 ios xcode swift

报错信息截图:
enter image description here

当我单击“如果您忘记了用户名/密码,单击此处以将包含您的用户名/密码的验证消息发送到您的电子邮件地址的段落中的“单击此处”按钮时”,弹出对话框说“MailCompositionService 意外退出”,这个消息就是我要解决的错误。

这句话下面的类是具有“单击此处”按钮的页面的 Swift 代码,该按钮生成我要解决的错误。它是 ViewController 类。

import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate
{
    @IBOutlet weak var ClickHereSendEmailButton: UIButton!
    @IBOutlet weak var Label: UILabel!
    @IBOutlet weak var UsernameTextField: UITextField!
    @IBOutlet weak var PasswordTextField: UITextField!
    @IBOutlet weak var SignInButton: UIButton!

    @IBAction func PressedSignInButton(sender: UIButton)
    {
        if UsernameTextField.text == username && PasswordTextField.text == password
        {
            // create the alert
            let alert = UIAlertController(title: "Correct", message: "Your credentials are correct.", preferredStyle: UIAlertControllerStyle.Alert)

            // add an action (button)
            alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default, handler: nil))

            // show the alert
            self.presentViewController(alert, animated: true, completion: nil)

            Label.text = "The credentials are correct."
            UsernameTextField.resignFirstResponder()
            PasswordTextField.resignFirstResponder()
        }
        else
        {
            // create the alert
            let alert = UIAlertController(title: "Incorrect", message: "Your credentials are incorrect.", preferredStyle: UIAlertControllerStyle.Alert)

            // add an action (button)
            alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default, handler: nil))

            // show the alert
            self.presentViewController(alert, animated: true, completion: nil)

            Label.text = "The credentials are not correct."
            UsernameTextField.resignFirstResponder()
            PasswordTextField.resignFirstResponder()
        }
    }

    @IBAction func PressedClickHereSendEmailButton(sender: UIButton)
    {
        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(["nurdin@gmail.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

    func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }
}

这是 AppDelegate 类

import UIKit


@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate
{
    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Override point for customization after application launch.

        return true
    }
}

创建账户页面:

import UIKit

var username = ""
var password = ""
var email = ""

class CreateAccountPage: UIViewController
{
    @IBOutlet weak var UserNameTextField: UITextField!
    @IBOutlet weak var PasswordTextField: UITextField!
    @IBOutlet weak var EmailAddressTextField: UITextField!
    @IBOutlet weak var ActivateButton: UIButton!
    @IBOutlet weak var ReturnButton: UIButton!

    @IBAction func PressedActivateButton(sender: UIButton)
    {
      username = UserNameTextField.text!
      password = PasswordTextField.text!
      email = EmailAddressTextField.text!

        // create the alert
        let alert = UIAlertController(title: "Activated", message: "Your new account is now activated.", preferredStyle: UIAlertControllerStyle.Alert)

        // add an action (button)
        alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default, handler: nil))

        // show the alert
        self.presentViewController(alert, animated: true, completion: nil)
    }

    @IBAction func ReturnButton(sender: UIButton)
    {
    }

    //performSegueWithIdentifier("mySegueID", sender: nil
}

编辑:我还附上了两张截图,所以请查看它们以明确说明。 编辑 #2:我会发布更多 Storyboard链接的屏幕截图,但我只能发布两个,所以让我知道,我会通过电子邮件发送给他们。

最佳答案

MailCompositionService 意外退出 错误是一个模拟器错误。在真实设备上运行您的邮件发送代码进行测试。

关于ios - swift 错误 : "MailCompositionService quit unexpectedly",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35783581/

相关文章:

ios - 如何在XCTest中运行 "single test case"?

swift - 不支持将某些协议(protocol)用作符合另一个协议(protocol)的具体类型

ios - 跟踪UIButton状态变化的解决方案

ios - Xcode 中的 gRPC 库出现 "Control may reach the end of non-void function"错误

xcode - 在 Xcode 6.2 中调试

ios - EXC_BAD_ACCESS(代码=1)但仅在使用 IOS7.1b4 升级到 Xcode 5.1b4 后的设备上

ios - 是否可以创建与 Arduino 设备无线通信的 iOS 应用程序?

ios - NS谓词问题

Swift 4 : Find value in nested, 动态字典 <String, Any> 递归

swift - 如何在 Swift 中打印请求 NSMutableURLRequest