MFMessageComposeViewController

Hello Eveyrone,

Hope you are all healthy.
Has anyone used MFMessageComposeViewController on Xojo?
I am wondering how can i pass recipients and body to this class.

Link to Dev Apple
https://developer.apple.com/documentation/messageui/mfmessagecomposeviewcontroller

Thank you in advance

Try this: found it as an old project so not sure if it still works. Copy the class out of the Extensions module and update the Modules folder with the latest copy of iOSKit: github.com/kingj5/iOSKit .

https://www.dropbox.com/s/sylsjp5qaqhargb/MFMessageComposeViewController.xojo_binary_project?dl=1

[quote=481531:@Dave S]Here is some old SWIFT code I wrote… it might give you some information

[code]
private func sendEmail() {
var flag : Bool=false
#if targetEnvironment(simulator)
StatusMsg(“EMAIL”,“Service not available via Simulator”)
return
#endif
if MFMailComposeViewController.canSendMail() {
flag=true //(“Can send email.”)
let controller = MFMailComposeViewController()
controller.mailComposeDelegate = parent as? MFMailComposeViewControllerDelegate
//Set the subject and message of the email
controller.setSubject(jobName)
controller.setMessageBody(“Here is the PDF Document you requested.”, isHTML: false)
if let fileData = try? Data(contentsOf: URL(fileURLWithPath: documentPATH)) {
controller.addAttachmentData(fileData, mimeType: “application/pdf”, fileName: “BingoCards”)
} else {
flag=false // failed
StatusMsg(“EMAIL”,“Unable to attach document.”)
}
if flag { parent?.present(controller, animated: false, completion: nil) }
} else {
StatusMsg(“EMAIL”,“Email service not available”)
}

}
[/code][/quote]
He is looking for the Message variety for text messages, not emails. But email is also included in iOSKit already :slight_smile:

1 Like

Thank you both very very much for your fast replies and help.
I used Jason’s project and it seems to work fine.
Jason I believe you should add it again on your latest iOSKit as it may be helpful for anyone else.

Stay strong and safe.
At your disposal anytime.
Sebastian

Jason,

Found this thread helpful and have an app almost ready to go in which sending an email is essential element.
If I tap my send email button I get MFMessageComposeViewController and it send emails wonderfully. If I tap Cancel button in MFMessageComposeViewController and chose Delete Draft it works. But if I try to send an email again the MFMessageComposeViewController view does not work properly. The Cancel and send buttons do not work. Keyboard is not operating properly. Have to quit app and restart to restore proper operation.

Make sure you nil out/delete the view controller after displaying it. They are single use only and must be re-created to send another email. So if you drag one onto a view for example you will have this problem.

Jason,
Thanks for quick reply. I had, in fact, dragged one on to the view. So I deleted that and added to send button code:
“Dim MFMessageComposeViewController1 as new MFMessageComposeViewController”
Now the compose view does not close when I tap the send icon though the email is sent.

You need to add a property to the view of type MFMessageComposeViewController. In your button something like this:

myMailView = new MFMessageViewController
// prepare whatever is needed and display

Then when it is closed do

myMailView = nil

I think that should do it if my memory is correct

Jason,
Thanks. It works great now. The question I had was how does one know when the email view is closed.
For future users here is the complete setup.
In ViewA there is a property myMailViewController as Extensions.MFMessageViewController and a button Send Email. I have added a line of code to the Activate event of View A - myMailViewController=nil - which should run when the email view disappears.
Some of the code in the Send Email button is:

//clear any old view controllers just in case
MyMailViewController=nil
MyMailViewController = new Extensions.MFMailComposeViewController

MyMailViewController.setToRecipients Array(recip)
MyMailViewController.setSubject(“Report”)
MyMailViewController.setMessage(theMessage)

if MyMailViewController.canSendMail then
MyMailViewController.PresentInView(self)
else
MsgBox “Email is not available.”
end if

1 Like

I just created a pull request on Github that allows getting the MFMailComposeResult information back.

1 Like

Thanks Jeremie, I merged your pull request.

1 Like