
This is the first application I wrote and published to the App Store. It was purely a learning exercise to follow an idea through from start to navigating App Store review. Undoubtedly, the most interesting part of this early project was the fact that I had to dip into UIViewControllerRepresentable in order to support emailing functionality that I wanted.
struct MailView: UIViewControllerRepresentable {
@Environment(\.presentationMode) var presentation
@Binding var data: ComposeMailData
let callback: MailViewCallback
class Coordinator: NSObject, MFMailComposeViewControllerDelegate {
@Binding var presentation: PresentationMode
@Binding var data: ComposeMailData
let callback: MailViewCallback
init(presentation: Binding<PresentationMode>,
data: Binding<ComposeMailData>,
callback: MailViewCallback) {
_presentation = presentation
_data = data
self.callback = callback
}
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult,
error: Error?) {
if let error = error {
callback?(.failure(error))
} else {
callback?(.success(result))
}
$presentation.wrappedValue.dismiss()
}
}
func makeCoordinator() -> Coordinator {
Coordinator(presentation: presentation, data: $data, callback: callback)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<MailView>) -> MFMailComposeViewController {
let vc = MFMailComposeViewController()
vc.mailComposeDelegate = context.coordinator
vc.setSubject(data.subject)
vc.setToRecipients(data.recipients)
vc.setMessageBody(data.message, isHTML: false)
data.attachments?.forEach {
vc.addAttachmentData($0.data, mimeType: $0.mimeType, fileName: $0.fileName)
}
vc.accessibilityElementDidLoseFocus()
return vc
}
func updateUIViewController(_ uiViewController: MFMailComposeViewController,
context: UIViewControllerRepresentableContext<MailView>) {
}
static var canSendMail: Bool {
MFMailComposeViewController.canSendMail()
}
}
What is this a nice thing to do to myself as a new Swift and SwiftUI developer? No.
Did I get the emailing functionality working? Yes.