Reputation: 1888
I want to use a MFMailComposeViewController() in Swift 2.0 and Xcode 7.0.1. I fear, this is a duplicate question, but didn't find a solution.
My code:
@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)
}
and
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)
The error is thrown in print("Mail cancelled")
When I test this on my device (iPad) everything is fine. But when i use the simulator, i get the error
viewServiceDidTerminateWithError: Error
Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)"
UserInfo={Message=Service Connection Interrupted}
Mail cancelled
But I want to run this in simulator to see, how it looks i.e. on iPhone 6...
Upvotes: 2
Views: 4293
Reputation: 7290
You have to first check if the device can send mail with 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)
}
Upvotes: 2