Search code examples
swiftmfmessagecomposeviewcontroller

Send SMS in iOS


I am brand new in iOS developing and xCode. Previously i developed android and i know i should work with Intent to make call and send SMS in android App. Now i want to develop a simple App that when i press a Button it send a SMS to a specific number. So i installed a Mac OS 10.11 VM on my Ubuntu. And could connect connect my iPhone 5 to that and run my simple Hello World App in real iPhone Device and not simulator. Now i created a Button in StoryBoard and make a funcion by following this article : Send SMS Message Toturial

Also look at other links and was similar. Here is my simple ViewController.swift.

import UIKit
import MessageUI

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func sendMessage(sender: AnyObject) {

        print("Send button pressed") //display a text to make sure is calling

        if MFMessageComposeViewController.canSendText() {
            let controller = MFMessageComposeViewController()
            controller.body = "TestMessage "
            controller.recipients = ["xxxxxxxxx", "xxxxxxxxx"] // sending for two numbers
            controller.messageComposeDelegate = self
            self.presentViewController(controller, animated: true, completion: nil)
        }
    }
    func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {

        switch (result.rawValue) {
        case MessageComposeResultCancelled.rawValue:
            print("Message was cancelled")
            self.dismissViewControllerAnimated(true, completion: nil)

        case MessageComposeResultFailed.rawValue:
            print("Message failed")
            self.dismissViewControllerAnimated(true, completion: nil)

        case MessageComposeResultSent.rawValue:
            print("Message was sent")
            self.dismissViewControllerAnimated(true, completion: nil)

        default:
            break;
        }
    }

}

And also created a Simple Button and link that to sendMessage above. When i run the app, displays the button and when i press it, it seems it is doing something, but the SMS is not sent. Any Idea?? I really appreciate it.

Update : As our friend said i added print("Send button pressed") in sendMessage Button, so i find out the button call sendMessage when i press it.


Solution

  • I finally did it, I mixed up two solutions, First of all,i changed the function of sendMessage and renamed it to sendText, in this way :

        @IBAction func sendText(sender: AnyObject) {
        if (MFMessageComposeViewController.canSendText()) {
            let controller = MFMessageComposeViewController()
            controller.body = "Text Body"
            controller.recipients = ["xxxxxxxxxxx"]
            controller.messageComposeDelegate = self
            self.presentViewController(controller, animated: true, completion: nil)
        }
    }
    

    And the protocol of MessageComposeViewController is :

    func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
        switch (result.rawValue) {
        case MessageComposeResultCancelled.rawValue:
            print("Message was cancelled")
            self.dismissViewControllerAnimated(true, completion: nil)
        case MessageComposeResultFailed.rawValue:
            print("Message failed")
            self.dismissViewControllerAnimated(true, completion: nil)
        case MessageComposeResultSent.rawValue:
            print("Message was sent")
            self.dismissViewControllerAnimated(true, completion: nil)
        default:
            break;
        }
    }
    

    And then click on the button in storyboards, and press control key and drag the button to ViewController and select the sendText function, now it works correctly.

    Thx to my Friend @Kabiroberai for help and give some time.