Reputation: 101
I do the standard functionality of sending messages with MFMailComposeViewController
.
My code:
if MFMailComposeViewController.canSendMail()
{
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["[email protected]"])
mail.setSubject("Subject")
mail.setMessageBody("Some Text", isHTML: false)
self.presentViewController(mail, animated: true, completion: nil)
}
Controller do not open and I see a message in the console that have never seen.
[MC] Filtering mail sheet accounts for bundle ID: [My Bundle ID], source account management: 1
[MC] Result: NO
Help please.
Upvotes: 10
Views: 11897
Reputation: 161
You need to Login in 'Mail' App in order to present the Mail Composer.
Upvotes: 0
Reputation: 63
@IBAction func gmailButton(_ sender: UIButton){
self.popUp()
}
func popUp(){
guard MFMailComposeViewController.canSendMail() else {
return
}
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["[email protected]"])
composer.setSubject("succes")
composer.setMessageBody("succes sent", isHTML: false)
present(composer, animated: true)
}
First you have to log in to Gmail, after that this program can be used. don't forget use real device to try this
Upvotes: -1
Reputation: 3463
For Swift 3.0.1 - 4.2 Compatible
if MFMailComposeViewController.canSendMail()
{
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["[email protected]"])
mail.setSubject("Subject")
mail.setMessageBody("Some Text", isHTML: false)
self.present(mail, animated: true, completion: nil)
}
I had the same error though it works perfectly on my device with iOS 10.1.1. I had a similar problem and found that the Mail Composer would only work on iOS 9 in the simulator, there is some sort of bug with iOS 10 and running Mail Composer on the simulator with my current knowledge.
Update I have also tested this with a device with iOS 11.4 and got the same results.
Tried these calls to open mail on the simulator and they did not work. Though work they work fine on a real device.
UIApplication.shared.keyWindow?.rootViewController?.present(mail, animated: true)
self.navigationController?.present(mail, animated: true, completion: nil)
Upvotes: 1
Reputation: 1
For me this answer solved the problem. I had the same issue on a real device with active mail accounts and changed the presenting view controller to
let mailComposeViewController = MFMailComposeViewController()
mailComposeViewController.mailComposeDelegate = self
mailComposeViewController.setToRecipients([address])
mailComposeViewController.setMessageBody(message, isHTML: false)
mailComposeViewController.setSubject(subject)
UIApplication.shared.keyWindow?.rootViewController?.present(mailComposeViewController, animated: true)
Upvotes: -1
Reputation: 103
If you check the value of MFMailComposeViewController.canSendMail(), you will see that it is false. So that the code inside your if statement would not get executed. And to return true, you need to enable one email on your mobile phone.
Upvotes: 0
Reputation: 2326
If a mail account has been set at the device where you try to test your application there is no problem. Please create a mail account.
Upvotes: 2
Reputation: 298
Had the same problem. It was connected with presenter.
You need to present MFMailComposeViewController from UINavigationController. Try this:
self.navigationController.presentViewController(mail, animated: true, completion: nil)
It solved my problem.
Upvotes: 0