TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Sending email from inside the app

Forums > SwiftUI

Hello! I added to my app an in-app email. It works fine (on the real device of course), but in the console i get this printout: [PPT] Error creating the CFMessagePort needed to communicate with PPT.

does anyone know what it means and how can I solve that?

I just found out, that the emails are being sent, but it's also staying as a draft in the email app. I hope this information is useful for you.

Thanks in advance, regards, Martin

This is my code, to be correct, i found it and the credit goes of course to them who wrote it. Thanks a lot!

import Foundation
import MessageUI

class EmailHelper: NSObject, MFMailComposeViewControllerDelegate {
    public static let shared = EmailHelper()

    override init() {
        super.init()
    }

    func sendEmail(subject:String, body:String, to:String){
        if !MFMailComposeViewController.canSendMail() {
            print("No mail account found")
            return
        }

        let picker = MFMailComposeViewController()

        picker.setSubject(subject)
        picker.setMessageBody(body, isHTML: false)
        picker.setToRecipients([to])
        picker.mailComposeDelegate = self

        EmailHelper.getRootViewController()?.present(picker, animated: true, completion: nil)
    }

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        EmailHelper.getRootViewController()?.dismiss(animated: true, completion: nil)
    }

    static func getRootViewController() -> UIViewController? {
        UIApplication.shared.keyWindow?.rootViewController
    }
}

extension UIApplication {

    var keyWindow: UIWindow? {
        // Get connected scenes
        return self.connectedScenes
            // Keep only active scenes, onscreen and visible to the user
            .filter { $0.activationState == .foregroundActive }
            // Keep only the first `UIWindowScene`
            .first(where: { $0 is UIWindowScene })
            // Get its associated windows
            .flatMap({ $0 as? UIWindowScene })?.windows
            // Finally, keep only the key window
            .first(where: \.isKeyWindow)
    }

}

I call the function with this button:

 Button {
                        EmailHelper.shared.sendEmail(subject: "Requesting support", body: "App: \(bundleName())\n\(versionAndBuildNumber())\nI need help with the following problem: \n\n\n", to: "xyz@gmail.com")
                    } label: {
                        Text("Email to support")
                    }

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.