报错信息截图:
当我单击“如果您忘记了用户名/密码,单击此处以将包含您的用户名/密码的验证消息发送到您的电子邮件地址的段落中的“单击此处”按钮时”,弹出对话框说“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/