Sending Email from SwiftUI Problems

Viewed 881

I have been using UIViewControllerRepresentable with MFmailComposer to pre-populate an email message from a SwiftUI view. I am getting the following error in the console when sending the message:

2020-10-02 09:27:58.559227-0400 CC0 Notes2[1288:471808] [PPT] Error creating the CFMessagePort needed to communicate with PPT.

In spite of the error, the message sends successfully. On IPadOS I get a Swoosh to show that the mail was sent, but on iOS I get a successful mail send but no Swoosh sound to confirm the send. Is this error a bug? I am using XCode 12.0.1, iPadOS 14.0.1 and iPOS 14.0.1

If i can not get a Swoosh out of iOS, how can I tap into the send result to confirm to the user that the mail has been sent?

Using this code:

struct MailView: UIViewControllerRepresentable {
    @EnvironmentObject var theBody: GlobalData
    var imageData: NoteImageData
    @State private var ishowingSuccess = false
    
    @Binding var isShowing: Bool
    @Binding var result: Result<MFMailComposeResult, Error>?

    class Coordinator: NSObject, MFMailComposeViewControllerDelegate {

        @Binding var isShowing: Bool
        @Binding var result: Result<MFMailComposeResult, Error>?

        init(isShowing: Binding<Bool>,
             result: Binding<Result<MFMailComposeResult, Error>?>) {
            _isShowing = isShowing
            _result = result
        }

        func mailComposeController(_ controller: MFMailComposeViewController,
                                   didFinishWith result: MFMailComposeResult,
                                   error: Error?) {
            defer {
                isShowing = false
            }
            guard error == nil else {
                self.result = .failure(error!)
                return
            }
            self.result = .success(result)
        }
    }

    func makeCoordinator() -> Coordinator {
        return Coordinator(isShowing: $isShowing,
                           result: $result)
    }

    func makeUIViewController(context: UIViewControllerRepresentableContext<MailView>) -> MFMailComposeViewController {
        let vc = MFMailComposeViewController()
        vc.mailComposeDelegate = context.coordinator
        vc.setSubject("")
        //vc.setMessageBody(imageData.description , isHTML: true)
        vc.setMessageBody( theBody.mailBody, isHTML: true)
        return vc
    }

    func updateUIViewController(_ uiViewController: MFMailComposeViewController,
                                context: UIViewControllerRepresentableContext<MailView>) {

    }
}

and calling the email:

.sheet(isPresented: $isShowingMailView) {
                        MailView(imageData: imageData, isShowing: self.$isShowingMailView, result: self.$result )
                    }
0 Answers
Related