Send SMS Message to multiple recipients in Swift - ios

I want to send a single message to multiple recipients using MFMessageComposeViewController, I am unable to add the second and so on recipients
here's my code :
#IBAction func sendSms(sender: AnyObject) {
if (MFMessageComposeViewController.canSendText())
{
let controller = MFMessageComposeViewController()
controller.body = self.textView.text
controller.recipients = [self.phoneField.text!]
controller.messageComposeDelegate = self
self.presentViewController(controller, animated: true, completion: nil)
}
else
{
print("Error")
}
}
extension MyViewController : MFMessageComposeViewControllerDelegate
{
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult)
{
controller.dismissViewControllerAnimated(true, completion: nil)}
}
}
Any Suggestions how can i send it to multiple recipients?

If user is entering multiple phone numbers in your textfield then you need to prompt the user to separate phone numbers using comma (or any other symbol) through which you can separate the string and add it into your array, then pass that array in controller.recipients
here is an example
if (MFMessageComposeViewController.canSendText())
{
let controller = MFMessageComposeViewController()
controller.body = self.textView.text
let phoneNumberString = "123456789,987654321,2233445566"
let recipientsArray = phoneNumberString.components(separatedBy: ",")
controller.recipients = recipientsArray
controller.messageComposeDelegate = self
self.presentViewController(controller, animated: true, completion: nil)
}
else
{
print("Error")
}

If you want to add multiple recipients then you can write this...
here is my code...
if (MFMessageComposeViewController.canSendText())
{
let composeVC = MFMessageComposeViewController()
composeVC.messageComposeDelegate = self
// Configure the fields of the interface.
composeVC.recipients = ["4085551212","8597485365","2564756984"]
composeVC.body = "Hello from California!"
// Present the view controller modally.
self.presentViewController(composeVC, animated: true, completion: nil)
}
func messageComposeViewController(controller: MFMessageComposeViewController,
didFinishWithResult result: MessageComposeResult)
{
// Check the result or perform other tasks.
// Dismiss the message compose view controller.
controller.dismissViewControllerAnimated(true, completion: nil)
}

Related

How to add PDF attachment while sending sms in Swift

if (MFMessageComposeViewController.canSendText())
{
let controller = MFMessageComposeViewController()
controller.body = ""
controller.recipients = ["1234567890"]
controller.messageComposeDelegate = self as? MFMessageComposeViewControllerDelegate self.present(controller, animated: true, completion: nil)
}
else
{
print("Cannot send the message")
}
func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult)
{
//Displaying the message screen with animation.
self.dismiss(animated: true, completion: nil)
}
while sending sms i need to attach 2 pdf i know how to add on mail but for sms i haven't found any documentation.
i have used this way for mail but how to do on messaging.
func sendEmail() {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setSubject("Invoice PDF")
do {
let attachmentData = NSData(contentsOfFile: pdfFilePath)
mail.addAttachmentData(attachmentData! as Data, mimeType: "application/pdf", fileName: "InvoicePDF")
mail.mailComposeDelegate = self
present(mail, animated: true)
}
} else {
// show failure alert
}
}
You can export files using the UIActivityViewController:
let activityVC = UIActivityViewController(activityItems: [your-data-here], applicationActivities: nil)
self.present(activityVC, animated: true, completion: nil)
If you want to limit the export options you can use the excludedActivityTypes property.

MFMessageViewController turns to white screen when presented

I have presented MFMessageViewController with following code.
extension TransactionResultViewController {
func showMessageComposeController() {
if MFMessageComposeViewController.canSendText() {
let messageComposeVc = MFMessageComposeViewController()
messageComposeVc.body = viewModel.offlinePayload
messageComposeVc.recipients = ["37245"]
messageComposeVc.messageComposeDelegate = self
self.present(messageComposeVc, animated: true, completion: nil)
}
}
}
and dismissed with following code.
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
controller.dismiss(animated: true, completion: nil)
}
Sometimes the MFMessageviewcontroller is not presented and sometimes it shows blank white/black screen. Please help. Thanks

Can't get app to dismiss the mail compose view controller & return to first screen

I'm using the Swift book to try to learn to code. I added the delegate method to dismiss the view, but it's not working. What am I missing here?
#IBAction func emailButtonTapped(_ sender: UIButton) {
if !MFMailComposeViewController.canSendMail() {
print("Can not send mail")
return
}
guard MFMailComposeViewController.canSendMail() else { return }
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients(["example#example.com"])
mailComposer.setSubject("Look at this")
mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)
present(mailComposer, animated: true, completion: nil)
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
dismiss(animated: true, completion: nil)
}
}
You just need to declare the delegate function outside of the IBAction:
#IBAction func emailButtonTapped(_ sender: UIButton) {
if !MFMailComposeViewController.canSendMail() {
print("Can not send mail")
return
}
guard MFMailComposeViewController.canSendMail() else { return }
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients(["example#example.com"])
mailComposer.setSubject("Look at this")
mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)
present(mailComposer, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
dismiss(animated: true, completion: nil)
}

There's a bug in CNContactPickerController

After spending all day (>12 hours) trying to isolate a bug in 13 lines of mind-bogglingly generic code, I have come to the dubious conclusion that there must be a bug in the current iteration of CNContactPickerViewController, in iOS 9.2.
Simply copy+paste this ViewController and link the invite action to a button.
The bug is that MFMessageComposeViewController dismisses itself immediately.
If anybody knows what to do with this, do share?
import UIKit
import MessageUI
import ContactsUI
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate, CNContactPickerDelegate {
let contactPickerVC = CNContactPickerViewController()
let messageVC = MFMessageComposeViewController()
override func viewDidLoad() {
super.viewDidLoad()
contactPickerVC.delegate = self
}
func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {
if let phoneNumberValue = contact.phoneNumbers.first?.value as? CNPhoneNumber {
if let phoneNumber = phoneNumberValue.valueForKey("digits") as? String {
// Configure message ViewController
messageVC.messageComposeDelegate = self
messageVC.recipients = [phoneNumber]
messageVC.body = "Yoyoyo"
picker.presentViewController(messageVC, animated: true, completion: nil)
}
}
}
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
#IBAction func invite(sender: AnyObject) {
presentViewController(contactPickerVC, animated: true, completion: nil)
}
}
I got it working by dismissing the pickerVC and changing the controller which presents the messageVC!
Insert (before the messageVC config lines):
picker.dismissViewControllerAnimated(true, completion: nil)
Replace
picker.presentViewController(messageVC, animated: true, completion: nil)
with
presentViewController(messageVC, animated: true, completion: nil)

button from app opens email but won't close the window and return to app

I have a button in my app which opens up an email to be sent to me, when this button is pressed the email app on iPhone opens up and when sent is pressed the email is sent however the window doesn't close and then return to my app. Also when i press cancel it gives the option to save/delete draft but again doesn't close the window and return to my app. I have attached the email code below.
#IBAction func SendMessage(sender: AnyObject) {
var mail: MFMailComposeViewController!
let toRecipients = ["usalim76#gmail.com"]
let subject = "Enquiry"
let body = "Your body text"
mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(toRecipients)
mail.setSubject(subject)
mail.setMessageBody(body, isHTML: true)
presentViewController(mail, animated: true, completion: nil)
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
controller.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
}
looks like you forgot to implement the MFMailComposeViewControllerDelegate, add this:
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
controller.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
Managed to fix it!
#IBAction func SendMessage(sender: AnyObject) {
let mailComposeViewController = configuredMailComposeViewController()
if MFMailComposeViewController.canSendMail() {
self.presentViewController(mailComposeViewController, animated: true, completion: nil)
} else {
self.showSendMailErrorAlert()
}
}
func configuredMailComposeViewController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property
mailComposerVC.setToRecipients(["someone#somewhere.com"])
mailComposerVC.setSubject("Sending you an in-app e-mail...")
mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)
return mailComposerVC
}
func showSendMailErrorAlert() {
let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
sendMailErrorAlert.show()
}
// MARK: MFMailComposeViewControllerDelegate Method
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
}

Resources