Last active
April 24, 2024 07:31
-
-
Save dabodamjan/21d1cba80fcd83d1268c41e39d378882 to your computer and use it in GitHub Desktop.
Sending a contact us email on iOS 14 (can be also used with 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
// | |
// Feel free to use this code in your project. | |
// Inspired by SO answer https://stackoverflow.com/a/65743126 | |
// Uses DeviceKit as a dependency https://github.com/devicekit/DeviceKit | |
// | |
import Foundation | |
import MessageUI | |
import DeviceKit | |
class MailComposeViewController: UIViewController, MFMailComposeViewControllerDelegate { | |
static let shared = MailComposeViewController() | |
func sendContactUsEmail() { | |
let email = Constants.Email.contactEmailAddress | |
let subject = Constants.Email.contactEmailSubject | |
let body = String( | |
format: Constants.Email.body, | |
Device.current.description, | |
Device.current.systemVersion?.description ?? "Unknown", | |
UIApplication.appVersion ?? "Unknown") | |
if MFMailComposeViewController.canSendMail() { | |
sendWithMailApp(email: email, subject: subject, body: body) | |
} else { | |
sendWith3rdPartyEmailClient(email: email, subject: subject, body: body) | |
} | |
} | |
// MARK: - MFMailComposeViewControllerDelegate | |
func mailComposeController( | |
_ controller: MFMailComposeViewController, | |
didFinishWith result: MFMailComposeResult, | |
error: Error? | |
) { | |
controller.dismiss(animated: true, completion: nil) | |
} | |
// MARK: - Private funcs | |
private func sendWithMailApp(email: String, subject: String, body: String) { | |
let mail = MFMailComposeViewController() | |
mail.mailComposeDelegate = self | |
mail.setToRecipients([email]) | |
mail.setSubject(subject) | |
let encodedBody = body.replacingOccurrences(of: "/n", with: "<br>") | |
mail.setMessageBody(encodedBody, isHTML: true) | |
UIApplication.shared.windows.last?.rootViewController?.present(mail, animated: true, completion: nil) | |
} | |
private func sendWith3rdPartyEmailClient(email: String, subject: String, body: String) { | |
guard | |
let encodedParams = "subject=\(subject)&body=\(body)" | |
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)? | |
.replacingOccurrences(of: "/n", with: "%0D%0A") | |
else { return } | |
let urlString = "mailto:\(email)?\(encodedParams)" | |
guard | |
let url = URL(string: urlString) | |
else { return } | |
UIApplication.shared.open(url) | |
} | |
} | |
enum Constants { | |
struct Email { | |
static let contactEmailAddress = "[email protected]" | |
static let contactEmailSubject = "App feedback" | |
static let body = "/n/n——Important debug data——/nDevice: %@ /niOS: %@ /nApp version: %@/n———————————————" | |
} | |
} | |
extension UIApplication { | |
static var appVersion: String? { | |
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment