0

I'm trying to develop an app, that take a number from a variable then open in "Messages" app with (1500) in the field To: and the variable value in Text Messages field like this I tried this answer how to open an URL in Swift3 and Swift: How to open a new app when uibutton is tapped but i didn't figure out the URL for Messages app

what should I use? Big thanks.

Community
  • 1
  • 1
Mzoch
  • 133
  • 1
  • 2
  • 9

2 Answers2

0

use this

    if MFMessageComposeViewController.canSendText() == true{
        let recipients:[String] = ["1500"]
        var messageController = MFMessageComposeViewController()
        //messageController.messageComposeDelegate = self // implement delegate if you want
        messageController.recipients = recipients
        messageController.body = "Your_text"
        self.present(messageController, animated: true, completion: nil)

    }
  • thank you, it was really helpful. but for some reason it didn't appear, I implement delegate and import `MessageUI` with no errors but still not appear after clicking the button. – Mzoch Dec 27 '16 at 07:36
0

You need to import "MessageUI" to your class and use the below code.

func sendMessages() {   
    if MFMessageComposeViewController.canSendText() == true {
        let recipients:[String] = ["9895249619"]
        let messageController = MFMessageComposeViewController()
        messageController.messageComposeDelegate  = self
        messageController.recipients = recipients
        messageController.body = "Your_message_text"
        self.present(messageController, animated: true, completion: nil)
    } else {
        //handle text messaging not available
    }
}

func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {    
    controller.dismiss(animated: true, completion: nil)
}
Sujith Chandran
  • 2,671
  • 1
  • 15
  • 13