Lou Valencia
Lou Valencia

Reputation: 188

Sending an SMS/iMessage programmatically in Swift

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.

Upvotes: 3

Views: 6660

Answers (1)

Au Ris
Au Ris

Reputation: 4679

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

Upvotes: 3

Related Questions