- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
While developing an iOS application you might have got a scenario where you require to send a text message and you would be baffling around with Why? How? And What?
In this tutorial we will be focussing on how to send text message from your iOS application in Swift, where we will be sending a text message from your user’s phone number. While we cannot do this directly without the content of your user’s but we can display a precomposed message for the user to send which user can modify later if he wants to.
So let’s get started,
We will be using “MFMessageComposeViewController” class object to display the standard message composition interface inside your application.
Before we present the composition interface, we will populate the fields with the basic initial message which we wish to send, list of recipients which a user can modify later.
The composition interface does not guarantee the delivery of your message, it only lets you construct the initial message and present it for user approval. The user may opt to cancel the composition interface, in which case the message and its contents are discarded. If the user opts to send the message, the Messages app takes on the responsibility of sending the message.
Step 1: Open Xcode → New Projecr → Single View Application → Let’s name it “MailComposer”
Step 2: Open Main.storyboard and add a button and name it “Send Mail”, do not forget to add constraints “Horizontally and Vertically in the container” and add “width = 100 points and height to 50 points” as show below.
Step 3: Navigate to ViewController.swift and add the button instance IBAction and name it “sendMessage”
@IBAction func sendMessage(_ sender: Any) { }
Step 4: Add “MFMessageComposeViewControllerDelegate” protocol to your ViewController class, and add stub method to ensure it conforms to that protocol. Import “MessageUI”in ViewController.swift
Step 5: Create new method “displayMsgInterface” below the viewDidLoad and paste the below code, which will be responsible to present the MFMessageComposeViewController and configuration of prefilled recipients and message.
func displayMsgInterface() { let messageCompose = MFMessageComposeViewController() messageCompose.messageComposeDelegate = self // Configure recipients messageCompose.recipients = ["9401234567"] // Configure message body messageCompose.body = "Hey! I just learned how to send message using iOS App." // Present the ViewController modally if MFMessageComposeViewController.canSendText() { self.present(messageCompose, animated: true, completion: nil) } else { print("You cannot send messages.") } }
Step 6: Add the following code to the delegate method “didFinishWithResult”
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) { self.dismiss(animated: true, completion: nil) }
Step 7: Call “displayMsgInterface” method from “viewDidLoad”.
Your final code should look like below!
And we’re done! Let’s run it.
To see it running you need to run in real device as text messaging behaviour is not available in simulator.
- Related Articles
- If you got a chance to slap someone, then whom do you slap and why?
- How would you choose between two characteristics to be used for developing a hierarchy in classification?
- If you got a chance to make 3 changes in our law system, then what will you change and why?
- Why could you not use an elastic measuring tape to measure distance? What would be some of the problems you would meet in telling someone about a distance you measured with such a tape?
- You might have observed on a dry day that when you touch the screen of a television or computer monitor with picture tube you get a slight shock. Why does it happen?
- What precautions would you take if lightning occurs while you are outside the house?
- How would you distinguish experimentally between an alcohol and a carboxylic acid?
- While travelling on a rickshaw, you might have experienced that if the seat cover is very smooth, you tend to slip when brakes are applied suddenly. Explain.
- Suppose you are outside your home and an earthquake strikes. What precaution would you take to protect yourself?
- Does the transfer of energy take place when you push a huge rock with all your might and fail to move it? Where is the energy you spend going?
- Have you ever been in a jail? If yes, what have you faced?
- What would you do if you were made a superhero for a day?
- What would you observe if you place a magnet on a cork and float the cork in a trough filled with water?
- How would you separate a mixture of sugar and salt?
- you might have seen a karate player breaking off a pile on slabs in a single blow . have you ever noticed the positioning of his hand ? does this reminds you of a wedge?
