Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Send Text Message in Swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.40k
    Comment on it

    Hi,
     
    Here we’ll learn how to send messages programatically in Swift. With the use of provided MessageUI framework we will implement message functionality in our app. This framework has MFMessageComposeViewController class which provides an interface to send message. We can use this controller to display a user interface inside our app.
    Let’s learn how to send messages:


    1- Create an instance of MFMessageComposeViewController.

    let messageVC = MFMessageComposeViewController()

     
    2- Now check if your device can send messages or not. It’s a good practice to call this method before presenting the MFMessageComposeViewController.

    MFMessageComposeViewController.canSendText()

     

    3- Now conform the delegate methods to handle the listeners.

    messageVC.delegate = self
    messageVC.messageComposeDelegate = self

     
    4- Populate all necessary fields. with their values.
    Example: message body, recipients phone numbers . You can edit all these once controller is present.

    messageVC.body = "Enter a message";
    messageVC.recipients = [your phone number]// example [8888999663]
    // you can put multiple recipients because it's accepting an array.

     
    5- Present the message controller..

    self.present(messageVC, animated: true, completion: nil)


     
    6- Handle the user actions in delegate method.
     

    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
            switch (result) {
            case .cancelled:
                print("Message cancelled")
            case .failed:
                print("Message failed")
            case .sent:
                print("Message sent")
            }
            self.dismiss(animated: true, completion: nil)
        }

    Here you can dismiss the controller or do your stuff on any user action.
        
    Code Snippet:

    @IBAction func sendMessageButtonTapped(sender: AnyObject) {
            
            let messageVC = MFMessageComposeViewController()
            
            if MFMessageComposeViewController.canSendText(){
    
                messageVC.delegate = self
                messageVC.messageComposeDelegate = self
                messageVC.body = "Enter a message";
                messageVC.recipients = ["9760306003"]
                self.present(messageVC, animated: true, completion: nil)
    
            }
        }
    
    
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
            switch (result) {
            case .cancelled:
                print("Message cancelled")
            case .failed:
                print("Message failed")
            case .sent:
                print("Message was sent")
            }
            self.dismiss(animated: true, completion: nil)
        }

     

     

    Thank you

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: