Skip to content

Instantly share code, notes, and snippets.

@matteospada
Created October 8, 2022 15:36
Show Gist options
  • Select an option

  • Save matteospada/3bddeb7d157bcb87132778972735082c to your computer and use it in GitHub Desktop.

Select an option

Save matteospada/3bddeb7d157bcb87132778972735082c to your computer and use it in GitHub Desktop.
Support Email config
import MessageUI
//Check if user have mail configured.
if (MFMailComposeViewController.canSendMail()){
Button(action: {
self.isShowingMailView.toggle()
}) {
Text("Support").font(.system(size: 24)).foregroundColor(Color.gray).fontWeight(.bold).padding(.vertical)
}
.sheet(isPresented: $isShowingMailView) {
MailView(result: self.$result)
}
} else {
Text("Ask support to: me@example.com")
}
import SwiftUI
import UIKit
import MessageUI
struct MailView: UIViewControllerRepresentable {
@Environment(\.presentationMode) var presentation
@Binding var result: Result<MFMailComposeResult, Error>?
class Coordinator: NSObject, MFMailComposeViewControllerDelegate {
@Binding var presentation: PresentationMode
@Binding var result: Result<MFMailComposeResult, Error>?
init(presentation: Binding<PresentationMode>,
result: Binding<Result<MFMailComposeResult, Error>?>) {
_presentation = presentation
_result = result
}
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult,
error: Error?) {
defer {
$presentation.wrappedValue.dismiss()
}
guard error == nil else {
self.result = .failure(error!)
return
}
self.result = .success(result)
}
}
func makeCoordinator() -> Coordinator {
return Coordinator(presentation: presentation,
result: $result)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<MailView>) -> MFMailComposeViewController {
let vc = MFMailComposeViewController()
vc.mailComposeDelegate = context.coordinator
vc.setSubject("Support Request")
vc.setToRecipients(["matteo.spada@ymail.com"])
return vc
}
func updateUIViewController(_ uiViewController: MFMailComposeViewController,
context: UIViewControllerRepresentableContext<MailView>) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment