Sending SMS in iOS with Swift
Not sure if you really got the answer. I was in a similar hunt and came across this solution and got it to work.
import UIKit
import MessageUI
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
@IBOutlet weak var phoneNumber: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sendText(sender: UIButton) {
if (MFMessageComposeViewController.canSendText()) {
let controller = MFMessageComposeViewController()
controller.body = "Message Body"
controller.recipients = [phoneNumber.text]
controller.messageComposeDelegate = self
self.presentViewController(controller, animated: true, completion: nil)
}
}
func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
//... handle sms screen actions
self.dismissViewControllerAnimated(true, completion: nil)
}
override func viewWillDisappear(animated: Bool) {
self.navigationController?.navigationBarHidden = false
}
}
Send a text message directly from an iOS App
The answer us no without an API like Twilio, ZipWhip or Plivo. You can, however, create a message when the user taps a link via the "sms:" URL scheme. See https://developer.apple.com/library/content/featuredarticles/iPhoneURLScheme_Reference/SMSLinks/SMSLinks.html
Send SMS in iOS
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.
How to send a message in iOS
No, that is not available to directly send the messages with MFMessageComposeViewController
, it will follow the flow. It will present you with the ComposerView where you have to manually press the send button to send the SMS.
To use without composerView, you should try some third party API's which can be helpful.
Related Topics
Calculate Uitableviewcell Height to Fit String
Xcode/Simulator: How to Run Older iOS Version
Get Latitude/Longitude from Address
How to Use Settitletextattributes:Forstate in Uibaritem
Uiimagepickercontroller Camera View Rotating Strangely on iOS 8 (Pictures)
Xcode 6 Beta 7: Storyboard Adds Extra Space on Right and Left Sides
Setting Alpha on Uiview Sets the Alpha on Its Subviews Which Should Not Happen
Swift Read Userinfo of Remote Notification
Nsdictionaryofvariablebindings Swift Equivalent
Retrieve User Defaults Information from an Apple Watch
How to Delete an App from Itunesconnect/App Store Connect
Change Bundle Identifier in Xcode When Submitting My First App in iOS
Module Was Not Compiled for Testing' When Using @Testable
Wait for Async Task to Finish Completion Block Before Returning in App Delegate