Last active
April 1, 2022 11:32
-
-
Save mbernson/d0af14cd4d76ad694058940ac38b361e to your computer and use it in GitHub Desktop.
MFMailComposeViewController wrapped for SwiftUI
This file contains 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
// | |
// MailComposeView.swift | |
// | |
// Created by Mathijs Bernson on 08/03/2022. | |
// Copyright © 2022 Q42. All rights reserved. | |
// | |
import SwiftUI | |
import MessageUI | |
struct MailComposeView: UIViewControllerRepresentable { | |
let subject: String | |
let toRecipients: [String] | |
let body: String | |
@Environment(\.dismiss) private var dismiss | |
static var canSendMail: Bool { | |
MFMailComposeViewController.canSendMail() | |
} | |
func makeUIViewController(context: Context) -> MFMailComposeViewController { | |
let controller = MFMailComposeViewController() | |
controller.setSubject(subject) | |
controller.setToRecipients(toRecipients) | |
controller.setMessageBody(body, isHTML: false) | |
controller.mailComposeDelegate = context.coordinator | |
return controller | |
} | |
func updateUIViewController(_ uiViewController: MFMailComposeViewController, context: Context) { | |
// We don't update the MFMailComposeViewController | |
} | |
func makeCoordinator() -> Coordinator { | |
Coordinator(dismiss: dismiss) | |
} | |
class Coordinator: NSObject, MFMailComposeViewControllerDelegate { | |
private var dismiss: DismissAction | |
init(dismiss: DismissAction) { | |
self.dismiss = dismiss | |
} | |
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { | |
dismiss() | |
} | |
} | |
} |
This file contains 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
// | |
// SupportView.swift | |
// | |
// Created by Mathijs Bernson on 09/03/2022. | |
// Copyright © 2022 Q42. All rights reserved. | |
// | |
import SwiftUI | |
struct SupportView: View { | |
@State var emailRecipient = "info@q42.nl" | |
@State var emailSubject = "This is the subject" | |
@State var emailBody = "This is the body" | |
@State var isPresentingMailSheet = false | |
var body: some View { | |
if MailComposeView.canSendMail { | |
Button("Send us an email") { | |
isPresentingMailSheet = true | |
} | |
.sheet(isPresented: $isPresentingMailSheet) { | |
MailComposeView( | |
subject: emailSubject, | |
toRecipients: [emailRecipient], | |
body: emailBody | |
) | |
} | |
} else { | |
Text("This device cannot send mail.") | |
} | |
} | |
} | |
struct SupportView_Previews: PreviewProvider { | |
static var previews: some View { | |
SupportView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment