Я пытался использовать MailComposer. Вот код, который я использовал:
func setupMailer() {
if MFMailComposeViewController.canSendMail() {
emailController.mailComposeDelegate = self
emailController.setToRecipients([]) // set the email address
emailController.setSubject("BackgroundTask Test")
emailController.setMessageBody("Message body", isHTML: false)
}
}
И затем, когда пользователь нажимает кнопку:
func buttonPressed(button: UIButton) {
debugPrint("buttonPressed", button)
let path = dirpath.appendingPathComponent(filename)
let data = NSData(contentsOfFile: path.path)
emailController.mailComposeDelegate = self
emailController.addAttachmentData(data! as Data, mimeType: "text/csv", fileName: filename)
present(emailController, animated: true, completion: nil)
}
И при увольнении:
@objc func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
debugPrint("mailComposeController dismiss")
controller.dismiss(animated: true, completion: nil)
}
Обнаружено, что если кнопка нажата в первый раз, компоновщик почты работает нормально, независимо от того, выбираю ли я отправить или отменить.
Однако после того, как я отправлю/отменю во второй раз, составитель почты не может быть закрыт. У отправки есть ответ, который может отправить электронное письмо, но интерфейс компоновщика почты никогда не закрывается.
Я обнаружил, что функция func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
больше не срабатывает после первого раза.
Любые подсказки?