Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift错误:“MailCompositionService意外退出”_Ios_Xcode_Swift - Fatal编程技术网

Ios Swift错误:“MailCompositionService意外退出”

Ios Swift错误:“MailCompositionService意外退出”,ios,xcode,swift,Ios,Xcode,Swift,错误消息屏幕截图: 当我单击段落中的“单击此处”按钮时,如果您忘记了用户名/密码,请单击此处将包含您的用户名/密码的验证消息发送到您的电子邮件地址,对话框会弹出并显示MailCompositionService意外退出,这条消息就是我要解决的错误 这句话下面的这个类是页面的Swift代码,该页面有“单击此处”按钮,该按钮生成我想要解决的错误。它是ViewController类 import UIKit import MessageUI class ViewController: UIView

错误消息屏幕截图:

当我单击段落中的“单击此处”按钮时,如果您忘记了用户名/密码,请单击此处将包含您的用户名/密码的验证消息发送到您的电子邮件地址,对话框会弹出并显示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:我会发布更多故事板链接的截图,但我只能发布两个,所以请告诉我,我会发电子邮件给他们。

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

这是模拟器的问题,不是您的问题,尝试使用usb连接iPhone并从Xcode中选择它,然后运行应用程序,它应该可以正常工作,如果有错误,请告诉我们

您在模拟器上配置了邮件帐户吗?在尝试创建compose视图控制器之前,您应该检查canSendMail。我的模拟器上没有配置电子邮件帐户。你说的支票是什么意思。。。哦,就像你在if语句中说的,MFMailComposeViewController.canSendMail可能是nil或其他什么?我很困惑。是的,您创建了mail compose视图控制器,然后检查是否可以发送邮件。首先检查是否可以发送,如果可以,则可以创建compose view控制器。但是,正如其他人回答的那样,您无法从simulatorNo问题发送邮件。你接受了一个别人从我的答案中抄来的答案。问题是我是第一个回答正确的人。然后有人抄袭了我的答案并在我之后提交。没什么大不了的,但一般来说,你应该接受第一个正确答案,而不是被复制的答案。好吧,我现在点击了你的,这有什么用吗?