Sending an SMS/iMessage programmatically in Swift

6.4k views Asked by At

I’ve seen many things that mention using MessageUI Framework for COMPOSING messages, but is there a way to automate SENDING it in Swift? Perhaps even allowing my code to run once the Send Button is pressed, then I call the actual method to send it.

1

There are 1 answers

0
Au Ris On

You can't just send a message without user's confirmation, but you can present the MFMessageComposeViewController at any time you want.

import MessageUI

func sendSMS(with text: String) {
    if MFMessageComposeViewController.canSendText() {
        let messageComposeViewController = MFMessageComposeViewController()
        messageComposeViewController.body = text
        present(messageComposeViewController, animated: true, completion: nil)
    }
}