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")
}