我在网站和本网站上找到了代码。
我更新了已弃用的部分。
邮件似乎已发送但从未出现过。是否需要在应用程序外部设置某些内容或代码是否有问题。应用程序有一个发送电子邮件的按钮。
import Foundation
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
@IBAction func pressedSendMail(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(["myemail@hotmail.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 alert = UIAlertController(title: "Could Not Send Email",
message: "Your device could not send e-mail. Please check e-mail configuration and try again.",
preferredStyle: .Alert)
let action = UIAlertAction(title: "OK",
style: .Default,
handler: nil)
alert.addAction(action)
presentViewController(alert, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:4)
与电话和相机应用程序类似,电子邮件应用程序不会在模拟器中打开。您需要在物理设备上对其进行测试才能对其进行测试。从你给我们的代码看它应该工作正常。只需连接您的iPhone或其他Apple设备,然后在那里运行即可查看它。
答案 1 :(得分:0)
我正在测试一台设备(iPad mini),它不仅仅是没有发送电子邮件。原来我从未发过这个设备的电子邮件。所以邮件没有设置好。 iPhone会在这种情况下发出警告,而iPad则没有。如果您遇到类似问题,请检查您的用户是否已从设备发送电子邮件,而不仅仅是设备上的网络浏览器。