我想在 Swift 2.0 和 Xcode 7.0.1 中使用 MFMailComposeViewController()。我担心这是一个重复的问题,但没有找到解决方案。
我的代码:
@IBAction func sendMail(sender: AnyObject) {
let picker = MFMailComposeViewController()
picker.mailComposeDelegate = self
picker.setSubject(subject.text!)
picker.setMessageBody(body.text, isHTML: true)
presentViewController(picker, animated: true, completion: nil)
}
和
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
switch result.rawValue {
case MFMailComposeResultCancelled.rawValue:
print("Mail cancelled")
case MFMailComposeResultSaved.rawValue:
print("Mail saved")
case MFMailComposeResultSent.rawValue:
print("Mail sent")
case MFMailComposeResultFailed.rawValue:
print("Mail sent failure: \(error!.localizedDescription)")
default:
break
}
dismissViewControllerAnimated(true, completion: nil)
错误被抛出 print("邮件已取消")
当我在我的设备(iPad)上测试时,一切正常。但是当我使用模拟器时,我收到错误
viewServiceDidTerminateWithError: Error
Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)"
UserInfo={Message=Service Connection Interrupted}
Mail cancelled
但我想在模拟器中运行它来看看它在 iPhone 6 上的样子...
最佳答案
您必须首先检查设备是否可以使用MFMailComposeViewController.canSendMail()
发送邮件
@IBAction func sendMail(sender: AnyObject) {
guard MFMailComposeViewController.canSendMail()
else {
// TODO: Alert the user its device cannot send mail
print("Mail services are not available")
return
}
let picker = MFMailComposeViewController()
picker.mailComposeDelegate = self
picker.setSubject(subject.text!)
picker.setMessageBody(body.text, isHTML: true)
presentViewController(picker, animated: true, completion: nil)
}
关于ios-simulator - 如何通过模拟器使用 MFMailComposeViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32938348/