Created
October 8, 2022 15:36
-
-
Save matteospada/3bddeb7d157bcb87132778972735082c to your computer and use it in GitHub Desktop.
Support Email config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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